-->
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.  [ 2 posts ] 
Author Message
 Post subject: Cascade Delete Problem Using Hibernate's EntityManager
PostPosted: Tue Aug 12, 2008 8:33 am 
Newbie

Joined: Thu Jul 31, 2008 5:44 am
Posts: 9
Hi,
I am using Hibernate's EntityManager to persist a number of inter-related entities to the database. The entity relationships are as follows:
- Entity A Many-To-One relationship with Entity B (CascadeType = ALL)
- Entity A One-To-One relationship with Entity C (CascadeType = ALL)

I use the EntityManager to retrieve instances of entity A from the database. For each entry found, I call EntityManager.remove(A).

After removing all the entities I then proceed to synchronize the EntityManager with the database by calling EntityManager.flush(). I get an exception thrown at this point (see stack trace below).

HELP!!!!!!!!!!!

Code:
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637)
   at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:307)
   at com.tnsglobal.comtel.transmissionlogs.TransmissionLogMain.run(TransmissionLogMain.java:222)
   at com.tnsglobal.comtel.transmissionlogs.TransmissionLogMain.main(TransmissionLogMain.java:330)
Caused by: org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
   at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
   at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
   at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:222)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2484)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2702)
   at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:77)
   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:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:304)
   ... 2 more
Caused by: java.sql.BatchUpdateException: failed batch
   at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
   at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
   at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
   ... 15 more


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 12, 2008 5:14 pm 
Newbie

Joined: Thu Jul 31, 2008 5:44 am
Posts: 9
I had made wrong assumptions about the way the EntityManager removes entities. I was under the impression that a cascaded remove operation will delete related entities only if these entities are no longer referenced in the join tables implementing the relationships. This seems to me a sensible approach and I assumed this was the default behaviour. Barring performance issues I can't think of a good reason why the good people at Sun have not specidied this behaviour in the JPA specification. Then again I am no JPA guru :)

I was getting the exception listed in my previous post because the entity manager performs an unconditional remove on all entities in a cascade.ALL relationship. So I was accidentally deleting entities that were still beeing referenced. The workaround obviously was to not cascade remove operations and clean up what would effectively be "orphan" entities myself. Tedious!

I hope this helps anyone with similar problems.


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