Hi,
I have an many-to-many relationship mapped with annotations as below:
@Entity Class User {
// ...some attributes private Set<UserGroup> userGroups;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "User", cascade=CascadeType.ALL) @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) public Set getUserGroups() { return this.userGroups; } // getter/setter(s)
}
@Entity Class Group { // ...some attributes private Set<UserGroup> userGroups;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "Group", cascade=CascadeType.ALL) @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) public Set getUserGroups() { return this.userGroups; } // getter/setter(s) }
@Entity Class UserGroup { // ...some attributes private User user; private Group group;
@ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "userId", nullable = false, insertable = false, updatable = false) public User getUser() { return this.user }
@ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "groupId", nullable = false, insertable = false, updatable = false) public Group getGroup() { return this.group }
}
I am updating the UserGroup by doing:
user.getUserGroups().remove(userGroup1); entityManager.merge(user);
I am getting this exception:
Caused by: org.hibernate.ObjectDeletedException: deleted entity passed to persist: [UserGroup#<null>] at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:90) at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:644) at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:636) at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:323) at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268) at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216) at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169) at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296) at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242) at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219) at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169) at org.hibernate.engine.Cascade.cascade(Cascade.java:130) at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131) at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122) at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JTATransaction.commit(JTATransaction.java:135) at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54) at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:456)
If i remove cascade=CascadeType.ALL from Group entity i don't get any exception but i want to keep cascade in Group entity. Is there anything i need to do to get rid of this exception or I should remove cascade in Group entity?
Thanks,
|