[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-2421) Cascading Delete In Wrong Order

Steen Klingenberg (JIRA) noreply at atlassian.com
Wed Dec 5 06:02:56 EST 2007


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-2421?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_29056 ] 

Steen Klingenberg commented on HHH-2421:
----------------------------------------

We too have experienced that delete-orphan results in an unwanted UPDATE statement - before a DELETE is issued - causing a constraint violation an ultimately an exception...

org.hibernate.exception.ConstraintViolationException: could not update collection rows: [dk.jyskebank.kundesag.domain.Kundesag.dokumenter#28]

I believe it is a bug that the UPDATE statement is fired at all ? (I hope the enabling of optimistic locking through a version attribute has nothing to do with this ?)

I hope som Hibernate developer would at least comment on this and state wether it is recognized as a bug or is correct behavior.

We are using version 3.2.5 and DB2 for z/OS ver. 8.

> Cascading Delete In Wrong Order
> -------------------------------
>
>                 Key: HHH-2421
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2421
>             Project: Hibernate3
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 3.2.1
>         Environment: Hibernate 3.2.1, Java5, MySQL 5 (InnoDB)
>            Reporter: CannonBall
>            Priority: Trivial
>
> Mapping Document:
> <hibernate-mapping>
>     <class name="scratchpad.hibernate.A">
>         <id name="id">
>             <generator class="increment"/>
>         </id>
>         <list name="bs" cascade="all,delete-orphan">
>             <key column="bId"/>
>             <list-index column="idx"/>
>             <one-to-many class="scratchpad.hibernate.B"/>
>         </list>
>     </class>
>     <class name="scratchpad.hibernate.B">
>         <id name="id">
>             <generator class="increment"/>
>         </id>
>         <many-to-one name="a" column="aId" insert="false" update="false"/>
>         <many-to-one name="c" column="cId" not-null="false"/>
>     </class>
>     <class name="scratchpad.hibernate.C">
>         <id name="id">
>             <generator class="increment"/>
>         </id>
>     </class>
> </hibernate-mapping>
> Code between sessionFactory.openSession() and session.close():
>         long id;
>         SessionFactory factory = new Configuration().configure()
>                 .buildSessionFactory();
>         try {
>             Session s = factory.openSession();
>             try {
>                 Transaction tx = s.beginTransaction();
>                 try {
>                     C c = new C();
>                     s.save(c);
>                     B b = new B();
>                     b.setC(c);
>                     A a = new A();
>                     a.getBs().add(b);
>                     s.save(a);
>                     tx.commit();
>                     id = b.getId();
>                 } catch (Exception e) {
>                     try {
>                         tx.rollback();
>                     } catch (Exception e2) {
>                         // do nothing
>                     }
>                     throw e;
>                 }
>             } finally {
>                 s.close();
>             }
>             s = factory.openSession();
>             try {
>                 Transaction tx = s.beginTransaction();
>                 try {
>                     A a = (A) s.load(A.class, id);
>                     B b = a.getBs().get(0);
>                     a.getBs().remove(b);
>                     s.delete(b.getC());
>                     tx.commit();
>                 } catch (Exception e) {
>                     try {
>                         tx.rollback();
>                     } catch (Exception e2) {
>                         // do nothing
>                     }
>                     throw e;
>                 }
>             } finally {
>                 s.close();
>             }
>         } finally {
>             factory.close();
>         } 
> The generated SQL (show_sql=true):
> Hibernate: select max(id) from C
> Hibernate: select max(id) from A
> Hibernate: select max(id) from B
> Hibernate: insert into C (id) values (?)
> Hibernate: insert into A (id) values (?)
> Hibernate: insert into B (cId, id) values (?, ?)
> Hibernate: update B set bId=?, idx=? where id=?
> Hibernate: select a0_.id as id0_0_ from A a0_ where a0_.id=?
> Hibernate: select bs0_.bId as bId1_, bs0_.id as id1_, bs0_.idx as idx1_, bs0_.id as id1_0_, bs0_.aId as aId1_0_, bs0_.cId as cId1_0_ from B bs0_ where bs0_.bId=?
> Hibernate: select c0_.id as id2_0_ from C c0_ where c0_.id=?
> Hibernate: update B set cId=? where id=?
> Hibernate: update B set bId=null, idx=null where bId=?
> Hibernate: delete from C where id=?
> Hibernate: delete from B where id=? 
> When you have a collection that is mapped with a cascade of 'delete-orphan', when removing an entity from the collection, the corresponding orphan delete is scheduled at the end of the session's deletions queue.  As you can see from my example above, when you have a relationship of A has a list of B's, B has a relationship with C, removing B from the A's list results in its deletion after C's deletion (despite the order of statements dictating C's deletion after B's). If I were to make B's relationship to C not-null, the above code would result in a FK constraint error as C would be removed before B.
> You could force the correct removal of B before C with a manual delete of B like so:
>   A a = (A) s.load(A.class, id);
>   B b = a.getBs().get(0);
>   C c = b.getC();
>   a.getBs().remove(b);
>   s.delete(b);
>   s.delete(c); 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list