Hi,
In my domain model I have a bi-directionnel association between the ProductList entity and the Product entity with the following hibernate mapping :
@Entity @Indexed
@Table(name="product_list")
public class ProductList {
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name = "list_items",
inverseJoinColumns = { @JoinColumn(name = "product_id")},
joinColumns = { @JoinColumn(name = "list_id")})
@IndexColumn(name = "item_index", base = 1, nullable = false )
@LazyCollection(LazyCollectionOption.EXTRA)
@BatchSize(size=50)
private List<Product> products = new LinkedList<Product>();
....
}
@Entity
@Table(name="logical_item")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Product {
@ManyToMany(fetch=FetchType.LAZY, mappedBy="products")
private Set<ProductList> productLists = new LinkedHashSet<ProductList>();
...
}
But when I tried to add a product to a persistent productList Hibernate try to load all the product in the list before ! I have more than 14 000 products in a list !
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){
this.getProducts().add(item);
item.getProductLists().add(this);
}
How to add a product in a list without loading all the database ?