This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Deleting data from multiple tables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Deleting data from multiple tables?" Watch "Deleting data from multiple tables?" New topic
Author

Deleting data from multiple tables?

mallikarjun dontamsetti
Ranch Hand

Joined: Mar 18, 2011
Posts: 243

Hi i am practicing hibernate with some examples i come across this problem please help me
i want to delete data from two tables but in jsp i am getting first table primary key based on that i have to delete data from two tables (second one contain 1st table pk as fk) my error is


1640047 [[ACTIVE] ExecuteThread: '9' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO org.hibernate.event.def.DefaultDeleteEventListener - handling transient entity in delete processing
xxxxxxxxxxxxxxx
xxxxxxx--------------------------xxxxxxxx
Hibernate:
delete
from
jp.company_info
where
COMPANY_SN=?
1640063 [[ACTIVE] ExecuteThread: '9' for queue: 'weblogic.kernel.Default (self-tuning)'] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1451, SQLState: 23000
1640063 [[ACTIVE] ExecuteThread: '9' for queue: 'weblogic.kernel.Default (self-tuning)'] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot delete or update a parent row: a foreign key constraint fails (`jp`.`comp_contact_info`, CONSTRAINT `FK_COMP_COMPANY_SN` FOREIGN KEY (`COMPANY_SN`) REFERENCES `company_info` (`COMPANY_SN`))
1640063 [[ACTIVE] ExecuteThread: '9' for queue: 'weblogic.kernel.Default (self-tuning)'] ERROR org.hibernate.event.def.AbstractFlushingEventListener - 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:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
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 com.garnet.services.DAO.commit(DAO.java:40)
at com.garnet.services.CompanyMaintanceDAOImpl.deleteCompanyMaintance(CompanyMaintanceDAOImpl.java:101)
at com.garnet.admin.CompanyMaintanceServlet.doGet(CompanyMaintanceServlet.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3498)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(Unknown Source)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`jp`.`comp_contact_info`, CONSTRAINT `FK_COMP_COMPANY_SN` FOREIGN KEY (`COMPANY_SN`) REFERENCES `company_info` (`COMPANY_SN`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1693)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1108)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 24 more
Cannot delete or update a parent row: a foreign key constraint fails (`jp`.`comp_contact_info`, CONSTRAINT `FK_COMP_COMPANY_SN` FOREIGN KEY (`COMPANY_SN`) REFERENCES `company_info` (`COMPANY_SN`))


my code is:



I am using entities like this



please help me
mallikarjun dontamsetti
Ranch Hand

Joined: Mar 18, 2011
Posts: 243

Hy I got it i just added @Cascade(CascadeType.ALL) in the parent entity above child column. I work's fine for me.
mallikarjun dontamsetti
Ranch Hand

Joined: Mar 18, 2011
Posts: 243

Is there any problems will occur in future by using this annotation @Cascade please let me know if there any alternative thanks
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Why are you clearing your session after almost each statement?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
mallikarjun dontamsetti
Ranch Hand

Joined: Mar 18, 2011
Posts: 243

Wouter Oet wrote:Why are you clearing your session after almost each statement?


I am just beginner i have seen that in some web site Is It wrong..?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It isn't really wrong but it is certainly not right. As a beginner you'll probably never need session.clear().
mallikarjun dontamsetti
Ranch Hand

Joined: Mar 18, 2011
Posts: 243

ok thanks but when i apply same concept for update also it is saying that session is closed what may be the reason..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Deleting data from multiple tables?
 
Similar Threads
Flex - Blaze DS Service retry
weblogic admin server down
a different object with the same identifier value was already associated with the session: [ValueObj
Spring - BlazeDS Problem
Spring iBatis NullPointerException when accesing SQLMAP implemented class