-->
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.  [ 6 posts ] 
Author Message
 Post subject: Delete entity with relations
PostPosted: Tue Mar 18, 2008 2:38 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

I've the following entities relation, a Person has many Addresses:

Code:
public class Person{
  private String name;
  private Set<Address> addresses;
  ...

Code:
public class Address{
  private Person person;
  private String street;
  private String city;
  ...


Now, I would like to do a bulk delete of Person entities having name containing 'titi'. I would writte a query similar to this one:
Code:
  ...
  StringBuffer hql = new StringBuffer("delete from Person person where person.name like '%?%' ");


When the query will be executed, there will be parent FK violation has there are still addresses.

Question:
Do you know how (or if possible) I can remove in one statement (at least from hql) the person and related relations?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 12:55 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Did you try cascade option.

http://www.hibernate.org/hib_docs/reference/en/html/objectstate.html#objectstate-saveorupdate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 1:28 pm 
Newbie

Joined: Sun Jul 29, 2007 9:10 am
Posts: 8
sjhyam's right. You need to map the collection with cascade="delete" if you want associated addresses deleted as well. If you're not willing to use such an option you will have to manually delete the addresses first.

Cheers
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 2:34 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

Yeap, I've tried with this option which just run fine, when deleting entities by entities.
But what I want to achieve here is to do a bulk delete of entities and I'm just wondering if other association would be also deleted (if it's a behaviour I can except from Hibernate).

One way I could fix this is apparently by using 'ON DELETE CASCADE' on my table definition. But wanted to check with other users if my thought above is correct or wrong.

Rgds,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 3:36 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Of course you could explicitly delete all the adresses referencing such persons by a seperate statement

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject: Jira Issue regarding this
PostPosted: Wed May 21, 2008 4:15 am 
Newbie

Joined: Fri Feb 22, 2008 12:45 pm
Posts: 5
There is a JIRA Issue opened since July 2006 regarding this problem


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