-->
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.  [ 5 posts ] 
Author Message
 Post subject: Not an another cascade deletion issue...
PostPosted: Tue Feb 23, 2010 10:21 am 
Newbie

Joined: Tue Feb 23, 2010 9:59 am
Posts: 4
Hi all,

I got an issue about a cascading deletion :
I got a "Parent" table in which a column is a foreign key of a "child" table. There is a constraint on this foreign key : a value in the "child" table must match a value in the "parent" table.

My goal is to delete rows from the "child" table when a row from the "parent" table is deleted (rows linked by the foreign key from the "child" table).

I searched the web, and I noticed it's a common problematic and apparently easy to resolve, but each times I delete the "Parent" object, i got the following exception :
Code:
3187 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 2292, SQLState: 23000
3187 [main] ERROR org.hibernate.util.JDBCExceptionReporter - ORA-02292: violation de contrainte (TOM2_TEST1.FK_DELEGATION_DEMANDE) d'intégrité - enregistrement fils existant

3203 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not delete: [fr.gipcps.smtom.data.model.DemandeModifTitulaireVO#123]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2569)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2725)
   at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
   at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
   at org.springframework.test.AbstractTransactionalSpringContextTests.endTransaction(AbstractTransactionalSpringContextTests.java:355)
   at fr.gipcps.smtom.data.dao.impl.TraiterSuppressionDemandeModifTitulaireTest.testSupprimerDemandeModifTitulaire(TraiterSuppressionDemandeModifTitulaireTest.java:80)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at junit.framework.TestCase.runTest(TestCase.java:168)
   at junit.framework.TestCase.runBare(TestCase.java:134)
   at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
   at junit.framework.TestResult$1.protect(TestResult.java:110)
   at junit.framework.TestResult.runProtected(TestResult.java:128)
   at junit.framework.TestResult.run(TestResult.java:113)
   at junit.framework.TestCase.run(TestCase.java:124)
   at junit.framework.TestSuite.runTest(TestSuite.java:232)
   at junit.framework.TestSuite.run(TestSuite.java:227)
   at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02292: violation de contrainte (TOM2_TEST1.FK_DELEGATION_DEMANDE) d'intégrité - enregistrement fils existant

   at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:85)
   at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
   at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
   at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:455)
   at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:413)
   at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:1034)
   at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:194)
   at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:953)
   at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1222)
   at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3387)
   at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3468)
   at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1350)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2548)
   ... 35 more

the message is "Integrity constraint violation(TOM2_TEST1.FK_DELEGATION_DEMANDE) - existing son row"

Here is my Hibernate mapping :

Parent Table :
Code:
[...]
<set name="listeModifsUnitairesDelegations" inverse="true"
    table="MODIF_UNITAIRE_DELEGATION" cascade="all,delete-orphan">
    <key on-delete="cascade">
        <column name="NUMERO_DEMANDE" precision="10" scale="0" not-null="true" />
    </key>
    <one-to-many class="ModifUnitaireDelegationVO" />
</set>
[...]


Child Table :
Code:
[...]
<many-to-one name="demande" column="NUMERO_DEMANDE" foreign-key="FK_DELEGATION_DEMANDE"
    unique-key="UK_MODIF_DELEGATION" not-null="true" cascade="delete"
    class="DemandeModifTitulaireVO" />
[...]


In the java code, it's a simple 'delete' from my DAO.

Is it a problem about the order of the deletion ? Did I forget something into the mappings ? I really don't understand this issue…


Top
 Profile  
 
 Post subject: Re: Not an another cascade deletion issue...
PostPosted: Tue Feb 23, 2010 3:26 pm 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
The fact that the Child entity actually has a JAVA reference to the parent and thus you have mapped the set as inverse=true means that u are instructing Hibernate to treat the Child entity as the owner/maintainer of that relation. Thus u have to delete the children first.
Otherwise go for inverse=false but then maybe other parts of your code will not behave the way you expect.


Top
 Profile  
 
 Post subject: Re: Not an another cascade deletion issue...
PostPosted: Wed Feb 24, 2010 4:42 am 
Newbie

Joined: Tue Feb 23, 2010 9:59 am
Posts: 4
Thanks for your answer Tseringshrestha.

About the java reference to the parent from the child, it seemed me usual because 1 parent can have many children. I point that there is no join table in the model between these two tables.

But this solution seems being good, the reference on children will not be used for update or insert : children and parent are very independant (except that children must have parent).

However, i'll make more tests to be sure about the behaviour I expect.

Thanks again for your answer.


Top
 Profile  
 
 Post subject: Re: Not an another cascade deletion issue...
PostPosted: Wed Feb 24, 2010 5:05 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am just wondering why you have cascade="delete" in the <many-to-one> from the child to parent. It is not typical and maybe it is causing some kind of recursive problems since you also have cascade from the parent to the child.


Top
 Profile  
 
 Post subject: Re: Not an another cascade deletion issue...
PostPosted: Wed Feb 24, 2010 6:01 am 
Newbie

Joined: Tue Feb 23, 2010 9:59 am
Posts: 4
Indeed, it's an attribute i set for tests because i wanted delete the parent when he has no more childs, but here it's unusual because i disabled all foreign keys from other childs tables for tests.
But now, this attribute it's definitively removed.
I would not leave here ;)


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

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.