I am using Hibernate 2.1.2 and I have the following bi-directional parent/child mappings
<class name="Customer" table="CUST">
<id name="id" type="java.lang.Long" column="CUST_ID" unsaved-value="null">
<generator class="native">
<param name="sequence">SC01_SEQUENCE</param>
</generator>
</id>
<property name="customerNumber" type="java.lang.Long" >
<column name="CUST_NBR" not-null="false"/>
</property>
<bag name="addresses" inverse="true" cascade="all">
<key column="CUST_ID"/>
<one-to-many class="Address"/>
</bag>
</class>
<class name="Address" table="ADDR">
<id name="id" type="java.lang.Long" column="ADDR_ID" unsaved-value="null">
<generator class="native">
<param name="sequence">SE01_SEQUENCE</param>
</generator>
</id>
<many-to-one name="customer" column="CUST_ID" not-null="false"/>
<property name="streetAddressLine1">
<column name="STREET_LINE1" not-null="false"/>
</property>
</class>
Here is my Code:
Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
int i = session.delete("from Address");
System.out.println("Deleted: "+i);
tx.commit();
System.out.println("After Commit");
session.close();
When I run the above code I get the following error
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:2181)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:709)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1310)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.preFlushEntities(SessionImpl.java:2557)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2197)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2186)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at shaw.spectrum.core.MyMain.main(MyMain.java:32)
Here is my problem:
When I try to delete "from Customer", as expected, it deletes all customer rows and all address rows. When I try to delete "from Address", I want it to delete all rows in the child(address table) and leave the parent rows untouched. But I get the "Flush during cascade is dangerous" error.
When I set the cascade="delete-orphan", it actully work as intended ( deletes all rows in both tables when i delete from parent and
deletes all rows from the child table when i deletr from child). But this messes up my cascade save/updates.
I also tried using cascade="all-delete-orphan", but I get the same
"Flush during cascade is dangerous" error.
What am I missing? Thanks in advance.