-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Parent-Child cascade delete issue
PostPosted: Wed May 12, 2004 4:41 pm 
Newbie

Joined: Wed May 12, 2004 3:19 pm
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.