-->
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.  [ 5 posts ] 
Author Message
 Post subject: Avoiding subselect when querying many-to-many
PostPosted: Tue Jan 27, 2004 3:52 am 
Newbie

Joined: Tue Jan 27, 2004 3:37 am
Posts: 4
Location: Mount Maunganui, New Zealand
Hi, I'm sorry if this is a basic question but I've looked all over for the answer to this.

I'm trying to select lodgings (hotel, bed and breakfast, etc) based on features that a customer requests. I've modelled this as a Lodging class and a LodgingFeature class, both as entities in Hibernate with a many-to-many relationship between them.

This works fine until it comes time to selecting lodgings that match the customer's criteria. I've written the following query:

Code:
select l from Lodging l where elements(l.lodgingFeatures) in :features


I then set the features parameter as a java.util.List of LodgingFeature. This seems to work, the generated SQL looks something like this:

Code:
select lodging0_.id as id, ... from lodging lodging0_ where ((select lodgingf1_.lodging_feature_id from lodging_lodging_feature lodgingf1_ where lodging0_.id=lodgingf1_.lodging_id)in ? )


This SQL looks fine to me, my problem is that I'm using MySQL, which does not support subselects. Does anyone have any idea how I can avoid the subselect in this situation? Or will I have to use raw JDBC:

Code:
select lodging_id from lodging_lodging_feature where lodging_feature_id in (list of ids)


To query the junction table for the matching lodging IDs then use Hibernate to retrieve the objects (yuk).

Thanks in advance for any advice.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 3:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
How about something like

Code:
select distinct l from Lodging l join l.lodgingFeatures as feature where feature in :features

PS: Get a real Database :)


Top
 Profile  
 
 Post subject: Avoiding subselect when querying many-to-many
PostPosted: Tue Jan 27, 2004 4:28 am 
Newbie

Joined: Tue Jan 27, 2004 3:37 am
Posts: 4
Location: Mount Maunganui, New Zealand
gloeglm wrote:
How about something like

Code:
select distinct l from Lodging l join l.lodgingFeatures as feature where feature in :features



This looks good, it produces:

Code:
select distinct lodging0_.id as id ... from lodging lodging0_ inner join lodging_lodging_feature lodgingf1_ on lodging0_.id=lodgingf1_.lodging_id inner join lodging_feature lodgingf2_ on lodgingf1_.lodging_feature_id=lodgingf2_.id where (lodgingf2_.id in ? )


But then MySQL complains with:

[code]You have an error in your SQL syntax near ''


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 6:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Be sure to use query.setParameterList() or something like that . Show your calling code if you can't get it to work.

Quote:
PS: Get a real Database :)

I'd love to! Rudimentary transaction support, no referential integrity: It's almost sending me round the twist! But of course it is the most popular DB on the internet ;-}

I really think mySql holds up the positive developement of internet applications, everybody uses this thing just because it is most popular and nobody is able to utilize the great features of other databases ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 4:41 pm 
Newbie

Joined: Tue Jan 27, 2004 3:37 am
Posts: 4
Location: Mount Maunganui, New Zealand
gloeglm wrote:
Be sure to use query.setParameterList() or something like that . Show your calling code if you can't get it to work.


Fantastic, it works! Thanks very much for your help. For reference, the code ended up looking like:

Code:
Query q = session.createQuery("select distinct l from Lodging l join l.lodgingFeatures as feature where feature in (:features)");
q.setParameterList("features", features, Hibernate.entity(LodgingFeature.class));


gloeglm wrote:

Quote:
PS: Get a real Database :)


Quote:
I'd love to! Rudimentary transaction support, no referential integrity: It's almost sending me round the twist! But of course it is the most popular DB on the internet ;-}

I really think mySql holds up the positive developement of internet applications, everybody uses this thing just because it is most popular and nobody is able to utilize the great features of other databases ...


I have to agree. MySQL is only really useful for simple applications like blogging. For complex apps it doesn't support enough basic constructs to be useful. Oh well, perhaps I'll try and convince my client...


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