-->
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: HQl and associations
PostPosted: Fri Apr 23, 2004 3:00 pm 
Beginner
Beginner

Joined: Mon Feb 23, 2004 5:11 pm
Posts: 39
I have a customer table and a phone number table. A customer could have more than one phone number (home, mobile, work, evening etc..) and the Hibernate mapping files are set to bi-direcdtional 1 to many association.

For a given customer first name and the phone type (say "mobile"), I would like the hql to return a List of customers with the specific first name and the particular phone type associated with that customer.

i.e a list of customers where customer.name = "John" and for every customer returned the associated phone List should have only the mobile phone.

So when I use a query like this

List result = session.find("from Customer as cust join cust.phoneNumbers as phone where cust.firstName = ? and phone.type = ?", new Object[]{"John","Home"}, new Type[] {Hibernate.STRING, Hibernate.STRING});

The result contains a List of Object[2] where the first object is the customer and all its associated collections and the second object is phone number of the type specified in the where clause.

But I want the result to have list of customer objected with the associated phone list containing only those phones that match the where caluse.

Any help here would be appreciated. thanks.


Top
 Profile  
 
 Post subject: Try this...
PostPosted: Fri Apr 23, 2004 5:53 pm 
Beginner
Beginner

Joined: Sun Mar 28, 2004 7:18 pm
Posts: 26
Add "select cust" to the start of your query

You should then only get back the Customer objects. You can then traverse these objects to get your data.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 1:43 pm 
Beginner
Beginner

Joined: Mon Feb 23, 2004 5:11 pm
Posts: 39
When I do a select on customer, the customer object returned still has all phone numbers associated with it. But I only want the phone numbers that meet the criteria (a specific phone type)?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 1:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This is not possible, if you load an object, its associated collections are always in the state as stored in the Database. Use a collection filter to get the things you want.


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.