-->
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.  [ 4 posts ] 
Author Message
 Post subject: many-to-many query
PostPosted: Mon May 08, 2006 6:33 am 
Newbie

Joined: Thu May 04, 2006 5:18 am
Posts: 4
Location: Germany
Hello,

once again I have a question about a many-to-many association ;-).

I have an Event-Object, containing an array of Guest-Objects.
Boath are linked by the many-to-many accociaton. I used the many-to-many association as an Event-Objects contains many Guest-Objects and a Guest-Object can participate in many events. All that works fine.

Now I want to query for all Event-Objects, containing a certain guest.
For example I want to query for all events, which contain a guest with the name "Smith".

What do you think is the best way to query for this? Is it possible to get the Event-Object within one query (including sub-queries, joins, ...) or do I have to split the query in several seperate ones (E.g. get all Ids of the guests with the name "Smith", use this Ids to get all ....)?

Thank you for your answers

SunX


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 5:23 am 
Newbie

Joined: Thu May 04, 2006 5:18 am
Posts: 4
Location: Germany
Hi,

I still try to query for objects containing a certain object. I can't imagine that there isn't ANY way to solve my problem, but maybe I should try to descibe it more precisely.

I have one object, containing an array of a second object.

Code:
class Event
{
...
private String eventName;
private Array guests;
...
public Array Guests
{
get { return guests; }
set { guests = value; }
}
...
}

class Guest
{
private long id;
private String name;
...
public String Name
{
get ...
set ...
}
...
}


And the mapping file for the Event-Object


<array name="Event" table="event_guest" cascade="all">
<key column="eventid"/>
<index column="idx"/>
<many-to-many class="guest" column="guestid" />
</array>



Database Structure
Code:

event
----------------------------------------------
eventID  |     eventname
    5    |       Concert


event_guest
---------------------------------------------
eventid  |  guestid  |  idx
     5   |      1    |   0
     5   |      2    |   1
     5   |      3    |   2


guest
--------------------------------------------
guestid       |       name
     1        |       'Frank'                   
     2        |       'Smith'                   
     3        |       'Jonson'                   




Now I want to query for all events, the guest 'Smith' wants to visit. (In this example only the "Concert" event)

I could query for something like

Code:
"from Event as ev where ev.Guests[1].Name = 'Smith'"


That works fine :-), but I do not want to spezify the index. I need something indexless like

Code:
         
from Event as ev where ev.Guests[].Name = 'Smith'

Yes, that does not work, but does anyone has any idea how to do this.

Your help would be very appriciated

Thank you

SunX


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 7:09 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
You need to use a JOIN in your HQL:

Code:
SELECT ev FROM Event ev LEFT JOIN ev.Guests g WHERE g.Name = 'Smith'


Hope that helps.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 8:03 am 
Newbie

Joined: Thu May 04, 2006 5:18 am
Posts: 4
Location: Germany
that is exactly what I was looking for!

Thank you so much merge_s.rottem

:-)


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