-->
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: Newbie Question about Deleting Parent and Children
PostPosted: Mon Apr 18, 2005 9:44 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Hi all,

I have a newbie question, I have search in this newgroup and in the web for some answer and I haven't fould any. I have found many question of this type, but with no answer. By the way I am using Hibernate 3.0 with Spring 1.2RC1.

My question is very simple and probably trivial.

I have this POJO structure:

I have a Parent continaining a Set of Children in a one-to many pattern.

A Parent have this Set of Children :
-> child1
-> child2
-> child3

The database (Mysql in Dev, DB2 and Sybase in Prod) have a foreign key contrainst: we cannot delete the parent before we delete all children.

I wanna know if Hibernate can delete automatically, by setting some hibernate xml field, the set of children before deleting the parent. If not, what is the best way to delete the Set of children mannually before deleting the parent? I dont want to hardcode something that Hibernate may do easily.

Way of deleting the Set of Children "manually":

1) in a "on-delete" method in the Parent, - is it call before or after the parent delete?, I must read the doc here -, delete the Set the children one-by-one (or with a "delete from Children where parent=x" UQL statement).

2) Do it in some business DAO "deleteYourself" that will call the delete(this) atfer doing the Set delete.

3) other way?

Thanks all for answering.

Etienne
Montreal


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 9:55 am 
Regular
Regular

Joined: Tue Nov 23, 2004 7:42 am
Posts: 82
Location: London, England
In the Hibernate docs you'll see that relationships have a 'cascade' property. Here you can specify it to cascade your deletions.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 10:03 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Hi have done that already, doesn not work. The Cascade function is done after the deletion of the Parent, not before. I need it before.

Etienne.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 10:08 am 
Regular
Regular

Joined: Tue Nov 23, 2004 7:42 am
Posts: 82
Location: London, England
etienne wrote:
Hi have done that already, doesn not work. The Cascade function is done after the deletion of the Parent, not before. I need it before.

Etienne.


It works for me. Although to ensure this I had to make my foreign keys not null. What I've seen happen is that Hibernate will set the foreign keys on the related rows to null and then delete the parent before deleting those rows.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 10:14 am 
Regular
Regular

Joined: Tue Nov 23, 2004 7:42 am
Posts: 82
Location: London, England
I think I misunderstood your question.

You know that Hibernate can delete related rows but you want to control when.

I don't think there is an easy way to configure this in Hibernate. If you don't want to modify your existing code you could always write an interceptor to do it. Personally, I'm not keen on the concept of interceptors. I think the thing that put me off was the usage of 'instanceof' in the example. The use of that keyword should be avoided if possible.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 10:32 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Thank for your help. I have finnaly made it done.

The thing about setting the foreign key to "Null Allowed" was a good guess.

But I have notice that I cannot do an "executeQuery" of "delete from Job" because it will not use the "cascade=all" It seems that only a "delete(object)" will use the cascade all.

So this will not work because it will not use the "cascade=all" tag.

Code:
   public void deleteAllJob(){
      getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session){
                return new Integer(session.createQuery("delete Job").executeUpdate());
            }
        });
   }


But this one will work:
Code:
   public void deleteAllEachJob(){
      List l = getHibernateTemplate().loadAll(Job.class);
      Iterator i = l.iterator();
      while(i.hasNext()){
         Job j = (Job) i.next();
         getHibernateTemplate().delete(j);
      }
   }


The problem here is that it is a bit more slow than the batch delete.

Thanks

Etienne.


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.