-->
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: delete not propagated to join table in unidirectional 1-*
PostPosted: Tue Apr 01, 2008 11:54 am 
Newbie

Joined: Tue Apr 01, 2008 10:15 am
Posts: 4
EDIT: updated subject to better match problem description

Hello, I'm using Hibernate as a JPA provider and encounter the following problem. I've been reading documentation, and forum for hours bit still didn't find anything.

I have a unidirectional one-to-many relation with delete-orphan cascading.

I use the hbm2ddl script to genenerate the tables. This gives me 2 tables for the entities and 1 for the association table.

In a junit test, I try to merge a detached object on which I modified the one-to-many relation by removing some of the children. When committing I receive an integrity constraint violation (see below).

The constraint violation occurs because of the foreign key in the association table. One workaround is replacing the foreign key by one that has a CASCADE DELETE policy.

When I simply disable the constraints, the children are deleted correctly, but the association rows in the association table remain there.

So it seems the generated script should either add a cascading rule, or hibernate should delete the association rows before deleting the child entities.

Note that working with a bidirectional relation, everything is just fine.





Hibernate version: 3.2.1.ga
Mapping documents: annotation driven, I'm using Hibernate as a JPA provider
Name and version of the database you are using: oracle 9

Full stack trace of any exception that occurs:
org.springframework.transaction.UnexpectedRollbackException: JPA transaction unexpectedly rolled back (maybe marked rollback-only after a failed operation); nested exception is javax.persistence.RollbackException: Error while commiting the transaction
Caused by: javax.persistence.RollbackException: Error while commiting the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:433)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:662)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:632)
at org.springframework.test.AbstractTransactionalSpringContextTests.endTransaction(AbstractTransactionalSpringContextTests.java:293)
at com.bsb.sf.persistence.test.JPACarDAOTest.newTransaction(JPACarDAOTest.java:28)
at com.bsb.sf.persistence.test.JPACarDAOTest.testCascade(JPACarDAOTest.java:73)
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:585)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:47)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:115)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:180)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:153)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:111)
at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:174)
at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:254)
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.OldTestClassRunner.run(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
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: org.hibernate.exception.ConstraintViolationException: could not delete: [com.bsb.sf.persistence.car.model.Wheel#195529]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2491)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2647)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:74)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
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.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
... 33 more
Caused by: java.sql.SQLException: ORA-02292: integrity constraint (SFTEST.FK5FD89EF09D729A1D) violated - child record found

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:955)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1168)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3368)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2473)
... 44 more


Last edited by tom.monnier on Wed Apr 02, 2008 4:59 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 01, 2008 12:12 pm 
Newbie

Joined: Tue Apr 01, 2008 10:15 am
Posts: 4
Im testing with some sample code, the entities are Car and Wheels. Where a car has a one-to-many relationship to wheel.

some code snippets:

the relation declaration
Code:
   @OneToMany(cascade=CascadeType.ALL)
   @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)   
   public Collection<Wheel> getWheels() {
      return wheels;
   }


my junit test method
Code:
   public void testCascade() {
      Car car = new Car();
      Wheel wheel = new Wheel();
      car.addWheel(wheel);
      DAO.makePersistent(car);
      long id = car.getObjectId();

      newTransaction();

      car = new Car();
      car.setObjectId(id);
      car.setVersion(0);
      DAO.update(car);

      newTransaction(); //commits the transaction, here the exception is thrown
   }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 02, 2008 6:03 am 
Newbie

Joined: Tue Apr 01, 2008 10:15 am
Posts: 4
Done quite a lot of research, but stil have not found an obvious reason for this problem. It seems valid to me from a JPA point of view.

Does anyone know if this cascading is part of Hibernate responsibility, or that it perhaps is the responsibilty of the DBA (and thus the hbm2ddl script)?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 03, 2008 5:51 am 
Newbie

Joined: Tue Apr 01, 2008 10:15 am
Posts: 4
I don't know how I missed this problem, but when I simply add some wheels before the merge, this is not reflected in the db either.

I found a ticket for it at http://opensource.atlassian.com/project ... se/EJB-260 which is resolved as "cannot reproduce"


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.