-->
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: Delete "where exists" for child table
PostPosted: Mon Dec 05, 2005 2:44 pm 
Newbie

Joined: Tue Oct 25, 2005 12:32 pm
Posts: 13
Can somebody give me a Hibernate example of delete "where exists" usage? I want to delete all the rows from a child table based on a criteria in the parent. Something like this:

Code:
    DELETE FROM Child
    WHERE EXISTS 
    (SELECT 1 FROM Parent
    WHERE 
    Parent.field1 = 642 
    AND 
    Parent.field2 = 1 
    AND 
    Parent.pkid = Child.fkeyid)


I am using/evaluating Hibernate 3.1 rc3.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 10:27 am 
Newbie

Joined: Tue Oct 25, 2005 12:32 pm
Posts: 13
I figured this out so I wanted to post a reply in case someone else is looking for an example. In HQL

Code:
String hqlDelete = "delete from Child c where exists (from c.parent p where p.field1 = 642 AND p.field2 = 1)";
int numDeleted = session.createQuery(hqlDelete).executeUpdate();


Hibernate automatically adds the foreign key relationship between child and parent to the exists clause.


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.