-->
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.  [ 3 posts ] 
Author Message
 Post subject: ManyToOne and Delete
PostPosted: Sun Jan 10, 2010 9:49 am 
Beginner
Beginner

Joined: Mon Dec 29, 2008 3:25 pm
Posts: 20
Hello,
i have to classes A und B they have a ManyToOne dependency . If i delete A , all b objects should be delete too. How can I do this?

Code:
public class B{

   @ManyToOne(cascade = {CascadeType.ALL})
   @Override
   public A getA() {
      return a;
   }
   @Override
   public void setA(Aa) {
   this.a= a;
   }
}


But I gett follow exception
Code:
Caused by: 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 org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
   ... 42 more
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`verwaltung`.`b`, CONSTRAINT `FK951BD2B13A9AD6FA` FOREIGN KEY (`a_id`) REFERENCES `a` (`id`))
   at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2007)
   at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1443)
   at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
   at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
   ... 50 more


Top
 Profile  
 
 Post subject: Re: ManyToOne and Delete
PostPosted: Sun Jan 10, 2010 12:06 pm 
Newbie

Joined: Fri Jan 08, 2010 5:12 am
Posts: 4
Its because you are trying to violate the foreign key constraint. You need to delete the the child instance and then delete the parent. Alternatively you can use the cascade delete orphan option to delete the entire object graph of parent and child without deleting the child individually. But in case the child instances are referenced by other entities then you have no other option other than navigating the object graph and removing the references manually before deleting the instance. So try with the cascade delete orphan option first and then see if u are still getting an exception dig to the code. Hope this helps you :-)


Top
 Profile  
 
 Post subject: Re: ManyToOne and Delete
PostPosted: Mon Jan 11, 2010 10:59 am 
Newbie

Joined: Tue Jun 02, 2009 4:06 am
Posts: 16
Hi,

In your above example, B is the parent and A is the child.Hence if you delete A then B wont get deleted because theree might be another instances of A refering to B.Hence it throwns COntraint viiolation.

When ayou use a cascade-delete in that case when you delete a parent then all the child referncing to parent will be deleted.In your case if B is deleted then all A referring to B will be deleted but vice versa isnt possible.

Now in case of orphan-delet,it will delete all the child which doesnt have a parent.Fro example in your case if your delete an instance of B then all the instance/row of A referncing B will be deleted along with that all the instances of A which doesnt refer to B will also be deleted.
So this option also wont work.

Conclusion you cant delete B by just deleting A.

Hope it helps

Kartik


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