Issue
Lets say that we have an entity defined as follows:
public class Member extends User implements Comparable<Member> {
@ManyToMany
@JoinTable(name = "Friends")
@Filters({ @Filter(name = "deleted") })
private Collection<Member> friends = new HashSet<Member>();
}
and have a filter named deleted with no parameters defined like
@FilterDef(name = "deleted", defaultCondition = "deleted = 0")
The problem arises when I am trying to add a new element to the friends collection of a persistent Member object. After committing the transaction the following exception rises.
org.hibernate.HibernateException: cannot recreate collection while filter is enabled: [domain.entity.Member.friends#98304]
at org.hibernate.action.internal.CollectionUpdateAction.execute(CollectionUpdateAction.java:74)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:291)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
Any ideas what I am doing wrong?
Solution
I had the same problem. With me it was the combination of the @ManyToMany annotation with a java.util.List. It seems that Hibernate has a problem with recreating a Collection when the order might matter. I think that Hibernate does not check whether you actually put a Set or List into the Collection and therefore Hibernate checks the order. So there are two possible ways, I know of, to solve this. Either you put an extra annotation @IndexColumn(name = "NAME_OF_THE_INDEX_COLUMN") to your Collection
public class Member extends User implements Comparable<Member> {
@ManyToMany
@JoinTable(name = "Friends")
@Filters({ @Filter(name = "deleted") })
@IndexColumn(name = "NAME_OF_THE_INDEX_COLUMN")
private Collection<Member> friends = new HashSet<Member>();
}
or if the order does not matter to you, change the type to the interface Set.
public class Member extends User implements Comparable<Member> {
@ManyToMany
@JoinTable(name = "Friends")
@Filters({ @Filter(name = "deleted") })
private Set<Member> friends = new HashSet<Member>();
}
Hope this will help solve your problem.
Answered By - Bastian
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)