I have a demo to test deleting many-to-many entity,it like following:
Code:
//this setting is for entity Operation
<set name="users" table="T_UserRights" lazy="true" inverse="true" cascade="none">
<key column="OperID"/>
<many-to-many class="test.User" column="UserID" outer-join="auto"/>
</set>
//This setting is for entity User
<set name="operations" table="T_UserRights" lazy="true" cascade="none">
<key column="UserID"/>
<many-to-many class="test.Operation" column="OperID" outer-join="auto"/>
</set>
T_User: UserID UserName ...
T_Operation: OperID OperName ...
T_UserRights: UserID OperID
Now, I get a User instance from hibernate, and delete it, this code is:
Code:
//session is a variable of Session class
User user = (User) session.load(User.class, "test");
Transaction tx = session.beginTransaction();
session.delete(user);
session.flush();
tx.commit();
when the program run these codes. it throws the following exception:
Code:
net.sf.hibernate.HibernateException: Flush during cascade is dangerous - this might occur if an object was deleted and then re-saved by cascade (remove deleted object from associations)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2174)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:626)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1216)
at net.sf.hibernate.engine.Cascades$5.cascade(Cascades.java:105)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:280)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:373)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:296)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:331)
at net.sf.hibernate.impl.SessionImpl.preFlushEntities(SessionImpl.java:2470)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2188)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2177)
at test.Test.run(Test.java:132)
at test.Test.main(Test.java:38)
please help me! thanks!