-->
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 not working
PostPosted: Mon Mar 21, 2011 8:54 am 
Beginner
Beginner

Joined: Mon Nov 15, 2010 10:39 am
Posts: 27
Hi,

I have a problem with cascading delete.

I have a set of tables that are linked with bidirectional many-to-one/one-to-many, like this:

<class name=A table=A>
...
<set name="B" lazy="false" inverse="true" cascade="all">
<key column="b"/>
<one-to-many not-found="ignore" class="B"/>
</set>
</class>

<class name="B" table="B">
...
<many-to-one name="A"
column="a"
class="A"
lazy="false"
cascade="all"
/>
</class>

I would like any entry of B referenced by a certain entry in A to be deleted when the entry of A is deleted, but not the other way around (deleting an entry of B shouldn't delete any entries of A). At the moment this works the exact opposite, it cascades from B to A, but when i delete an element in A that is connected to an entry in B, i get the following error message:

javax.servlet.ServletException: org.hibernate.exception.ConstraintViolationException: could not delete: [xxx.persistence.persistentclasses.A#32768]
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:422)
com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

root cause

org.hibernate.exception.ConstraintViolationException: could not delete: [xxx.persistence.persistentclasses.A#32768]
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2710)
org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2893)
org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)

I have tried with different values for cascade and inverse, with no appearant difference. What am i doing wrong here?

- Tobb


Top
 Profile  
 
 Post subject: Re: Cascade delete not working
PostPosted: Mon Mar 21, 2011 10:10 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Set the inverse property of your OneToMany to false!
This way the collection which is currently handled by B objects will be handled by the parent A object.

And modify the Cascade.ALL from the ManyToOne to Cascade.MERGE (or any other Cascade Type that doesn't include Cascade.DELETE depending on your requirements)


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.