-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-many query
PostPosted: Mon Nov 14, 2005 10:24 pm 
With a many-to-many relationship where there are employees and addresses with a join table "AddressEmployee"


<class name="Employee" table="Employee">
......
<bag name="Addresses" table="AddressEmployees">
<key column="EmployeeId" />
<many-to-many class="Address" column="AddressId"/>
</bag>
</class>

What is the best way (best practice) to do a query returning Employees with an Address.City = "Sydney".

TIA


Top
  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 4:32 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
I'd do it something like this:

Code:
SELECT e FROM Employee e LEFT JOIN e.Addresses a WHERE a.City = 'Sydney'


Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 4:50 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Oops! :oops:

What I meant was:

Code:
SELECT e FROM Employee e INNER JOIN e.Addresses a WHERE a.City = 'Sydney'


If you use a left join you'll get all employees anyway! The above query should only return employees who have addresses that are in Sydney.

Cheers,

Symon.


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