-->
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.  [ 1 post ] 
Author Message
 Post subject: delete through executeUpdate with select id on same table
PostPosted: Tue Jul 31, 2012 12:57 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
It seems very hard to do this simple thing:
Delete a bunch of rows based on a select that involves a join with another table.

What I would expect to work
Code:
delete from Offer where offer.vendor = :vendor and entryDate < :olderThan and offer.validity.complete = false


This turns out to be illegal (using joins in a delete statement in Hibernate).

What works in some databases but not in MySQL. MySQL doesn't allow you to reference the same table in both a delete and a select statement (Ridiculous, I know.)
Code:
delete from Offer where id in (select offer.id from Offer offer where offer.vendor = :vendor and entryDate < :olderThan and offer.validity.complete = false)


What is the workaround that MySQL is proposing (translated from sql to hql)
Code:
delete from Offer where id in (select id from (select offer.id from Offer offer where offer.vendor = :vendor and entryDate < :olderThan and offer.validity.complete = false) as TMP)


Not surprisingly, this doesn't work in Hibernate. Is there any way to make this work as a simple statement in Hibernate? Or do I have to separate into two queries? Simple things should be simple.

Cheers,
Marc


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.