-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with cascade deletion of children in v2.1
PostPosted: Fri Dec 09, 2005 6:56 am 
Newbie

Joined: Thu Dec 08, 2005 11:44 am
Posts: 5
I'm new to this, and having a problem I don't understand...

I have two tables - Party and PartyNote. A Party may have many PartyNotes, and a PartyNote must belong to one Party. This is working fine, until I come to delete a Party.

What I want to happen is that all the child PartyNotes for that Party are deleted, and then the Party is deleted. I've added a "cascade" statement to try & define this behaviour.
What seems to happen, from the logs and the state of the data, is that it selects the child PartyNotes but does not delete them, and then it tries to delete the Party. And fails, so that I get an error : "Cannot delete or update a parent row: a foreign key constraint fails ".

Why is this? Have I made some foolish mistake in the mappings somewhere, or is it that I need a different version of Hibernate for this "cascade" function to work, or...?

Any help much appreciated!
Tracey
=============================================


Hibernate version:
Version 2.1.8

Mapping documents:

Party :
=============================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>

<class name="uk.co.trisystems.morph.crm.persistance.PartyDTO"
table="PARTY">
<meta attribute="class-description">
Details of a party. @author Tulna Chauhan
</meta>

<id name="pk" type="int" column="PARTY_PK">
<meta attribute="scope-set">protected</meta>
<generator class="native" />
</id>

<many-to-one name="parent"
class="uk.co.trisystems.morph.crm.persistance.PartyDTO"
column="PARENT_PARTY_ID" outer-join="true" cascade="none" />

<set name="partyNotes" lazy="true" cascade="all-delete-orphan">
<key column="PARTY_NOTE_PK"/>
<one-to-many class="uk.co.trisystems.morph.crm.persistance.PartyNoteDTO"/>
</set>



<property name="partyType" column="PARTY_TYPE"
type="uk.co.trisystems.morph.crm.user_type.PartyType">
<meta attribute="field-description">Party Type</meta>
<meta attribute="use-in-tostring">true</meta>
</property>

<property name="partyCode" column="PARTY_CODE" not-null="true"
type="string" unique="true" index="PARTY_CODE">
<meta attribute="field-description">Party Name</meta>
<meta attribute="use-in-tostring">true</meta>
</property>


<property name="partyName" column="PARTY_NAME" type="string">
<meta attribute="field-description">Party Name</meta>
</property>

<property name="status" column="STATUS"
type="uk.co.trisystems.morph.crm.user_type.PartyStatus">
<meta attribute="field-description">Status</meta>
<meta attribute="use-in-tostring">true</meta>
</property>


</class>

</hibernate-mapping>
=============================================

I did try:
=============================================
<set name="partyNotes" lazy="true" cascade="all-delete">
<key column="PARTY_NOTE_PK"/>
<one-to-many class="uk.co.trisystems.morph.crm.persistance.PartyNoteDTO"/>
</set>
=============================================
and variants with lazy="false", but to no avail.



PartyNote :

=============================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>

<class name="uk.co.trisystems.morph.crm.persistance.PartyNoteDTO"
table="PARTY_NOTE">
<meta attribute="class-description">
Details of a partyNote. @author Tracey Annison, after Tulna Chauhan
</meta>

<id name="pk" type="int" column="PARTY_NOTE_PK">
<meta attribute="scope-set">protected</meta>
<generator class="native" />
</id>

<many-to-one name="party" class="uk.co.trisystems.morph.crm.persistance.PartyDTO" column="PARTY_PK" not-null="true"/>

<property name="noteSubject" column="NOTE_SUBJECT" not-null="true"
type="string" >
<meta attribute="field-description">Note Subject</meta>
<meta attribute="use-in-tostring">true</meta>
</property>

<property name="noteText" column="NOTE_TEXT"
type="text">
<meta attribute="field-description">Note Text</meta>
</property>


<property name="noteDateTime" column="NOTE_DATE_TIME" not-null="true" type="timestamp">
<meta attribute="field-description">Timestamp</meta>
<meta attribute="use-in-tostring">true</meta>
</property>


</class>

</hibernate-mapping>
=============================================

Full stack trace of any exception that occurs:
=============================================
ERROR 10:27:20 org.springframework.web.servlet.FrameworkServlet.serviceWrapper():404 - Could not complete request
org.springframework.dao.DataIntegrityViolationException: (HibernateTemplate): data integrity violated by SQL ''; nested exception is java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails
java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:642)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:128)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2397)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at org.springframework.orm.hibernate.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:472)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:397)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:243)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at net.sf.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:80)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy14.deleteParty(Unknown Source)
at uk.co.trisystems.morph.crm.party.mvc.PartyDeleteFormController.onSubmit(PartyDeleteFormController.java:79)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:248)
at org.springframework.web.servlet.mvc.AbstractFormController.handleInvalidSubmit(AbstractFormController.java:538)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:230)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:675)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:623)
at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:384)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:353)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at uk.co.trisystems.morph.common.audit.AuditFilter.doFilter(AuditFilter.java:98)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at uk.co.trisystems.morph.common.filters.UrlLengthChecker.doFilter(UrlLengthChecker.java:66)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:300)
at net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:84)
at net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter.doFilter(SecurityEnforcementFilter.java:182)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
at net.sf.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:374)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
at net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:225)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
at net.sf.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:179)
at net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
=============================================

Name and version of the database you are using:
MySQL


Debug level Hibernate log excerpt:
=============================================
DEBUG 10:27:20 org.springframework.web.servlet.mvc.BaseCommandController.createCommand():270 - Creating new command of class [uk.co.trisystems.morph.crm.party.mvc.PartyFormBean]
DEBUG 10:27:20 org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission():247 - No errors -> processing submit
DEBUG 10:27:20 uk.co.trisystems.morph.crm.party.mvc.PartyDeleteFormController.onSubmit():61 - partyCode - AAAAAA
DEBUG 10:27:20 uk.co.trisystems.morph.crm.party.dao.PartyHibernateDAO.getPartyByPartyCode():136 - getting party by code AAAAAA
DEBUG 10:27:20 net.sf.hibernate.impl.BatcherImpl.log():230 - select partydto0_.PARTY_PK as PARTY_PK, partydto0_.PARENT_PARTY_ID as PARENT_P2_, partydto0_.PARTY_TYPE as PARTY_TYPE, partydto0_.PARTY_CODE as PARTY_CODE, partydto0_.PARTY_NAME as PARTY_NAME, partydto0_.STATUS as STATUS from PARTY partydto0_ where (partydto0_.PARTY_CODE=? )
DEBUG 10:27:20 net.sf.hibernate.impl.BatcherImpl.log():230 - select partydto0_.PARTY_PK as PARTY_PK, partydto0_.PARENT_PARTY_ID as PARENT_P2_, partydto0_.PARTY_TYPE as PARTY_TYPE, partydto0_.PARTY_CODE as PARTY_CODE, PARTY_NAME as PARTY_NAME, partydto0_.STATUS as STATUS from PARTY partydto0_ where (partydto0_.PARTY_CODE=? )
DEBUG 10:27:20 uk.co.trisystems.morph.crm.party.dao.PartyHibernateDAO.getPartyByPartyCode():141 - got parties [uk.co.trisystems.morph.crm.persistance.PartyDTO@b91881[pk=100008,partyType=COMPANY,partyCode=AAAAAA,status=New]]
DEBUG 10:27:20 uk.co.trisystems.morph.crm.party.mvc.PartyDeleteFormController.onSubmit():76 - party - uk.co.trisystems.morph.crm.party.domain.ConcreteParty@869e8f[pk:=100008,partyCode=AAAAAA,partyType=COMPANY]
DEBUG 10:27:20 uk.co.trisystems.morph.crm.party.dao.PartyHibernateDAO.deleteParty():87 - Deleting uk.co.trisystems.morph.crm.party.domain.ConcreteParty@869e8f[pk:=100008,partyCode=AAAAAA,partyType=COMPANY]
DEBUG 10:27:20 net.sf.hibernate.impl.BatcherImpl.log():230 - select partynotes0_.PARTY_NOTE_PK as PARTY_NO1___, partynotes0_.PARTY_NOTE_PK as PARTY_NO1_2_, partynotes0_.PARTY_PK as PARTY_PK2_, partynotes0_.NOTE_SUBJECT as NOTE_SUB3_2_, partynotes0_.NOTE_TEXT as NOTE_TEXT2_, partynotes0_.NOTE_DATE_TIME as NOTE_DAT5_2_, partydto1_.PARTY_PK as PARTY_PK0_, partydto1_.PARENT_PARTY_ID as PARENT_P2_0_, partydto1_.PARTY_TYPE as PARTY_TYPE0_, partydto1_.PARTY_CODE as PARTY_CODE0_, partydto1_.PARTY_NAME as PARTY_NAME0_, partydto1_.STATUS as STATUS0_, partydto2_.PARTY_PK as PARTY_PK1_, partydto2_.PARENT_PARTY_ID as PARENT_P2_1_, partydto2_.PARTY_TYPE as PARTY_TYPE1_, partydto2_.PARTY_CODE as PARTY_CODE1_, partydto2_.PARTY_NAME as PARTY_NAME1_, partydto2_.STATUS as STATUS1_ from PARTY_NOTE partynotes0_ left outer join PARTY partydto1_ on partynotes0_.PARTY_PK=partydto1_.PARTY_PK left outer join PARTY partydto2_ on partydto1_.PARENT_PARTY_ID=partydto2_.PARTY_PK where partynotes0_.PARTY_NOTE_PK=?
DEBUG 10:27:20 net.sf.hibernate.impl.BatcherImpl.log():230 - select partynotes0_.PARTY_NOTE_PK as PARTY_NO1___, partynotes0_.PARTY_NOTE_PK as PARTY_NO1_2_, partynotes0_.PARTY_PK as PARTY_PK2_, partynotes0_.NOTE_SUBJECT as NOTE_SUB3_2_, partynotes0_.NOTE_TEXT as NOTE_TEXT2_, partynotes0_.NOTE_DATE_TIME as NOTE_DAT5_2_, partydto1_.PARTY_PK as PARTY_PK0_, partydto1_.PARENT_PARTY_ID as PARENT_P2_0_, partydto1_.PARTY_TYPE as PARTY_TYPE0_, partydto1_.PARTY_CODE as PARTY_CODE0_, partydto1_.PARTY_NAME as PARTY_NAME0_, partydto1_.STATUS as STATUS0_, partydto2_.PARTY_PK as PARTY_PK1_, partydto2_.PARENT_PARTY_ID as PARENT_P2_1_, partydto2_.PARTY_TYPE as PARTY_TYPE1_, partydto2_.PARTY_CODE as PARTY_CODE1_, partydto2_.PARTY_NAME as PARTY_NAME1_, partydto2_.STATUS as STATUS1_ from PARTY_NOTE partynotes0_ left outer join PARTY partydto1_ on partynotes0_.PARTY_PK=partydto1_.PARTY_PK left outer join PARTY partydto2_ on partydto1_.PARENT_PARTY_ID=partydto2_.PARTY_PK where partynotes0_.PARTY_NOTE_PK=?
DEBUG 10:27:20 net.sf.hibernate.impl.BatcherImpl.log():230 - delete from PARTY where PARTY_PK=?
DEBUG 10:27:20 net.sf.hibernate.impl.BatcherImpl.log():230 - delete from PARTY where PARTY_PK=?
WARN 10:27:20 net.sf.hibernate.util.JDBCExceptionReporter.logExceptions():57 - SQL Error: 1217, SQLState: 23000
WARN 10:27:20 net.sf.hibernate.util.JDBCExceptionReporter.logExceptions():57 - SQL Error: 1217, SQLState: 23000
ERROR 10:27:20 net.sf.hibernate.util.JDBCExceptionReporter.logExceptions():58 - Cannot delete or update a parent row: a foreign key constraint fails
ERROR 10:27:20 net.sf.hibernate.util.JDBCExceptionReporter.logExceptions():58 - Cannot delete or update a parent row: a foreign key constraint fails
WARN 10:27:20 net.sf.hibernate.util.JDBCExceptionReporter.logExceptions():57 - SQL Error: 1217, SQLState: 23000
WARN 10:27:20 net.sf.hibernate.util.JDBCExceptionReporter.logExceptions():57 - SQL Error: 1217, SQLState: 23000
ERROR 10:27:20 net.sf.hibernate.util.JDBCExceptionReporter.logExceptions():58 - Cannot delete or update a parent row: a foreign key constraint fails
ERROR 10:27:20 net.sf.hibernate.util.JDBCExceptionReporter.logExceptions():58 - Cannot delete or update a parent row: a foreign key constraint fails
ERROR 10:27:20 net.sf.hibernate.impl.SessionImpl.execute():2400 - Could not synchronize database state with session
ERROR 10:27:20 net.sf.hibernate.impl.SessionImpl.execute():2400 - Could not synchronize database state with session
ERROR 10:27:20 org.springframework.web.servlet.FrameworkServlet.serviceWrapper():404 - Could not complete request
org.springframework.dao.DataIntegrityViolationException: (HibernateTemplate): data integrity violated by SQL ''; nested exception is java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails
=============================================

_________________
Cheers
Tracey


Top
 Profile  
 
 Post subject: Problem with cascade deletion of children
PostPosted: Fri Dec 09, 2005 9:09 am 
Newbie

Joined: Fri Jul 15, 2005 12:42 am
Posts: 18
I notice you have used

<set name="partyNotes" lazy="true" cascade="all-delete-orphan">
<key column="PARTY_NOTE_PK"/>
<one-to-many class="uk.co.trisystems.morph.crm.persistance.PartyNoteDTO"/>
</set>


If i read through your post correctly PARTY_NOTE_PK is the primary key of party notes and PARTY_PK is foriegn key to party table mapped as column party in the Bean.

key column in a set should be the foriegn key column in the child table namely party ; not the primary key

so the mapping must be

<set name="partyNotes" lazy="true" cascade="all-delete-orphan">
<key column="party"/>
<one-to-many class="uk.co.trisystems.morph.crm.persistance.PartyNoteDTO"/>
</set>

Hope this helps


Top
 Profile  
 
 Post subject: Nearly!
PostPosted: Fri Dec 09, 2005 10:44 am 
Newbie

Joined: Thu Dec 08, 2005 11:44 am
Posts: 5
Thank you for that, dilipr - It was very nearly the answer!

The exact text you gave wasn't quite it, though - the key column you define needs to be the actual column itself, not the bean name. This is how it needs to be :

In PartyNote: .
<many-to-one name="party" class="uk.co.trisystems.morph.crm.persistance.PartyDTO" column="PARTY_PK" />

In Party:
<set name="partyNotes" lazy="true" cascade="delete-orphan">
<key column="PARTY_PK"/>
<one-to-many class="uk.co.trisystems.morph.crm.persistance.PartyNoteDTO"/>
</set>


What happens then is that when you request the Party to be deleted, Hibernate does this :
- Locates the PartyNotes that are associated with it
- Sets the PARTY_PK of those PartyNotes to null
- Deletes those PartyNotes
- Deletes the Party

Hence, it is not possible to have the Party link in PartyNote have the property : not-null="true" If you try to set this, the process fails.

Thank you very much for your help - it set us thinking along the right lines, and now it works fine. Much appreciated!

_________________
Cheers
Tracey


Top
 Profile  
 
 Post subject: Problem with cascade deletion of children
PostPosted: Fri Dec 16, 2005 5:39 am 
Newbie

Joined: Fri Jul 15, 2005 12:42 am
Posts: 18
Hi Tracy
The problem that you mentioned
the Party link in PartyNote have the property : not-null="true"

this has been solved in the later versions. I am using v3.0.5 it working correctly.

Regards
Dilip


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.