During my domain model I've got a bi-directionnel association between your ProductList entity and also the Product entity using the following hibernate mapping :

@Entity @Indexed
public class ProductList {

@JoinTable(name = "list_items",
        inverseJoinColumns = { @JoinColumn(name = "product_id")},
        joinColumns = { @JoinColumn(name = "list_id")})
@IndexColumn(name = "item_index", base = 1, nullable = false )
private List<Product> products = new LinkedList<Product>();


public class Product {

@ManyToMany(fetch=FetchType.LAZY, mappedBy="products")
private Set<ProductList> productLists = new LinkedHashSet<ProductList>();


However when I attempted to include an item to some persistent productList Hibernate attempt to load all of the product within the list before ! I've a lot more than 14 000 items in a listing !

Product item = (Product) session.get(Product.class, 123);
ProductList myFavoriteItems = (ProductList) session.get(ProductList.class, 321);

// Evil lazy loading (need more 512Mo of memory )
myFavoriteItems.addItem(Product item);

public void addItem(Product item){

How you can give a product in a listing without loading all of the database ?

i suppose it is among the disadvantages of utilizing a ManyToMany relation when you really need to update the join table.

I'd advise creating an entity from the join table, and you would only needed to produce the join entity and save it :

public class ProductListItem {
    private Product product;

    private ProductList productList;


And also you could have a transient getter than would return an item list from the product :

public class Product {

    private Set<ProductListItem> items;

    public Set<ProductList> getProductLists() {
        Set<ProductList> list = new LinkedHashSet<ProductList>();
        for(ProductListItem item : items) {
        return Collections.unmodifiableSet(list);

Same factor for sleep issues from the manytomany relation.

Then, your save operation is only a matter of developing a ProductListItem and saving it, that will load nothing, and want just one place.

Be cautious together with your already established hql queries : when they used the hyperlink Product<->ProductList, they will not work any longer.

if you want to help keep the ManyToMany relation, you should think about : http://josephmarques.wordpress.com/2010/02/22/many-to-many-revisited/ (i have never attempted this solution)

public class Controller {

private static SessionFactory sf = HibernateUtil.getSessionFactory();

* @param args
public static void main(String[] args) {
// construct data
Item i1 = new Item("i1");
Item i2 = new Item("i2");
Item i3 = new Item("i3");
Category c1 = new Category("c1");

// get Category & i (i3)
Category c = (Category) sf.getCurrentSession().get(Category.class, c1.getId());
Item i = (Item) sf.getCurrentSession().get(Item.class, i3.getId());

// proxys i & c have null Set
System.out.println("i : " + i.getName());
System.out.println("c : " + c.getName());

// here we have the IDs
long category_id = c.getId();
long item_id = i.getId();


// add many to many data

// here we can use pure SQL to add a line in CATEGORY_ITEM.
// with the known IDs
String ins = "insert into category_items (item_id,category_id,item_index) SELECT 4639, 100, MAX(item_index)+1 from category_items where category_id = 100 ;";