-->
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: cascade="delete" Failed! foreign-key constraints
PostPosted: Wed Aug 09, 2006 7:51 am 
Newbie

Joined: Wed Aug 09, 2006 5:56 am
Posts: 2
Hi to all! I am a beginner in Hibernate and right now I am on the task of deleting a particular entity("Employee")
but upon deleting it the entities(empPassport, empPic, empAttachment, & empWorkExperience) which are collections
inside the said entity must also be deleted(delete-cascade).

In order to do this here is my Employee.hbm.xml mapping file:

<hibernate-mapping>
<class name="Employee" table="employee">
<id name="empId" column="employee_id" type="java.lang.Integer" >
<generator class="identity"/>
</id>

<!--PROPERTIES HERE-->
<property/>
...
<property/>

<!--Mapping for Set EmployeePassport-->
<set name="empPassport" inverse="true" lazy="true" cascade="delete">
<key column="employee_id" not-null="true"/>
<one-to-many class="EmployeePassport"/>
</set>

<!--Mapping for Set EmployeeWorkExperience-->
<set name="empWorkExp" inverse="true" lazy="true" cascade="delete">
<key column="employee_id" not-null="true"/>
<one-to-many class="EmployeeWorkExperience"/>
</set>

<!--Mapping for Set EmployeeAttachment-->
<set name="empAttachment" inverse="true" lazy="true" cascade="delete">
<key column="employee_id" not-null="true"/>
<one-to-many class="EmployeeAttachment"/>
</set>

<!--Mapping for EmployeePicture-->
<one-to-one name="empPic" class="EmployeePicture" cascade="delete"/>
</class>
</hibernate-mapping>

To further explain, as can be seen with my mapping for my class "Employee" which is to be persisted to table "employee",
I am trying to delete the employee records and at the same time delete all other records(empPassport, empPicture, empAttachment,
& empWorkExp) which is technically known as delete-cascade. These aformentioned records are collections inside "Employee". After building
and starting the tomcat all things are good meaning i have no error but upon hitting the delete button in my webpage causing
the invocation of delete function I now have this stack trace warnings and errors:

2006-08-09 16:42:00,368 WARN - JDBCExceptionReporter.logExceptions(71) | SQL Error: 1451, SQLState: 23000
2006-08-09 16:42:00,368 ERROR - JDBCExceptionReporter.logExceptions(72) | Cannot delete or update a parent row: a foreign key constraint fails (`eportal3/employee_passport`, CONSTRAINT `FKF2967A831229D0E0` FOREIGN KEY (`employee_id`) REFERENCES `employee` (`employee_id`))
2006-08-09 16:42:00,378 ERROR - AbstractFlushingEventListener.performExecutions(299) | Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
at com.exist.eportal.dao.hibernate.EmployeeDaoImpl.removeEmployee(EmployeeDaoImpl.java:88)
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:324)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
at $Proxy2.removeEmployee(Unknown Source)
at com.exist.eportal.employee.db.DBPimService.deleteEmployee(DBPimService.java:176)
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:324)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:165)
at $Proxy6.deleteEmployee(Unknown Source)
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:324)
at uk.ltd.getahead.dwr.impl.ExecuteQuery.execute(ExecuteQuery.java:170)
at uk.ltd.getahead.dwr.impl.DefaultProcessor.doExec(DefaultProcessor.java:552)
at uk.ltd.getahead.dwr.impl.DefaultProcessor.handle(DefaultProcessor.java:88)
at uk.ltd.getahead.dwr.DWRServlet.doPost(DWRServlet.java:178)
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 org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:292)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:79)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:143)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:138)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:50)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.securechannel.ChannelProcessingFilter.doFilter(ChannelProcessingFilter.java:168)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:174)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:246)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:220)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:173)
at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:120)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:39)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.exist.eportal.web.filter.MessageFilter.doFilter(MessageFilter.java:33)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
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:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:534)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`eportal3/employee_passport`, CONSTRAINT `FKF2967A831229D0E0` FOREIGN KEY (`employee_id`) REFERENCES `employee` (`employee_id`))
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:648)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 91 more

I hope someone has encountered(even if not as long as he can give me suggestions/solutions) the same experience and post a solution to this. Tnx!

Note: "employee_id" is the primary key of table employee and foreign key to the these tables-> employee_attachment,
employee_passport, and employee_work_experience.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 9:29 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
did you can try cascade="all-delete-orphan" on <set> element


Top
 Profile  
 
 Post subject: Same ERROR!
PostPosted: Wed Aug 09, 2006 9:46 pm 
Newbie

Joined: Wed Aug 09, 2006 5:56 am
Posts: 2
bkmr_77 wrote:
did you can try cascade="all-delete-orphan" on <set> element


I did try your suggestion bkmr_77 but I am still getting the same error. Tnx anyway!:)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 2:57 am 
Newbie

Joined: Tue Aug 15, 2006 2:53 am
Posts: 5
original:
<set name="empPassport" inverse="true" lazy="true" cascade="delete">
new:
<set name="empPassport" inverse="false" lazy="true" cascade="delete">


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 10:16 am 
Beginner
Beginner

Joined: Wed Dec 28, 2005 3:14 pm
Posts: 29
In many scenarios, I found that I can't rely on ORM cascading delete.
Since Hibernate does not take care the association management when setting up a new one ( you have to write the code at both side), how can we rely on it to remove one?


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.