I'm having troubles with an Hibernate Many To Many Collection. When i try to remove an item from the relation, hibernate loads the entire collection (is too big). Here is an example: supose A has many to many relation with B
public class A {
@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH }, fetch = FetchType.LAZY)
@JoinTable(name = "a_b", joinColumns = { @JoinColumn(name = "a_id", referencedColumnName = "id", unique = false) }, inverseJoinColumns = { @JoinColumn(name = "b_id", referencedColumnName = "id", unique = true) })
@Sort(type = SortType.COMPARATOR, comparator = AscBComparator.class)
private List<B> listB;
public List<B> getListB() {
return listB;
}
}
// in other part
A.getListB().remove(concreteB);
When the program reach to A.getListB() hibernate loads the entire collection. Is there anyway to avoid this? I just want to remove the row from the join table.
I tried with: @BatchSize to limit de collection size but finally i understood that it doesn't work as i expected.
@LazyCollection(LazyCollectionOption.EXTRA), didn't work
If the relation between A and B is bidirectional, fetch B, then update it change the value of its A, make this value null or another value. some thing like this :
B b = bService.getBById(id);
b.setA(null); // or b.setA(anotherValueOfA);
bService.update(b);
it should work