-->
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.  [ 9 posts ] 
Author Message
 Post subject: Query q: how to select parents based on child's value
PostPosted: Mon Apr 19, 2004 7:17 pm 
Newbie

Joined: Mon Apr 19, 2004 7:10 pm
Posts: 7
I have parent and child objects:

Code:
public class Parent
  {
  Set children = new HashSet();

  ...
   }

public class Child
  {
  String name;

  ...
   }

with all the appropriate setters and getters, mapping files, etc.

My question is about the HQSL syntax and capabilities.

I would like to select all Parent objects that have one or more children with the name "Ron".

I tried:

Code:
Parent parent where parent.children.name='Ron'


But that won't work.

Any tips appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 10:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
randy from az?

"parent.children" is a convenience syntax for joins across single-multiplicity associations. However, your Parent.children association defines a many-multiplicty association, so you have to specify the join explicitly. Try this:

from Parent as p inner join p.children as c where c.name = :childName


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 11:10 pm 
Newbie

Joined: Mon Apr 19, 2004 7:10 pm
Posts: 7
Yes, Tucson, AZ... How did you know?

Thanks for the reply.

I got some very interesting and unexpected results.

The exact query is

Code:
String shipname = "Extra photos";
List result = session.find( "from User as u inner join u.shipments as s where s.name = :shipName ", shipname, Hibernate.STRING);


The result of this is a List of arrays, each of which contains two Objects, a Parent (i.e., User) and a Child (i.e., Shipment).

For example, the following code calls the toString() method on each object:

Code:
iter = result.iterator();
while( iter.hasNext())
  {
  Object[] oa = (Object[])iter.next();
  System.out.println( oa.length);
  for (int i = 0; i < oa.length; i++)
    {
    System.out.println(oa[i]);
    }
  }


With the following output:
Code:
2
<User>Id: [40288134fc05daa300fc05daab0c0001], Name: [Randy Kahle], Email: [randy@foo.org], Shipments: [[<Shipment>Id: [40288134fc05daa300fc05daab480009], Name: [Trip Photos], Priority: [null]</Shipment>, <Shipment>Id: [40288134fc05daa300fc05daab4a000a], Name: [Extra photos], Priority: [null]</Shipment>]]</User>
<Shipment>Id: [40288134fc05daa300fc05daab4a000a], Name: [Extra photos], Priority: [null]</Shipment>


Questions:

* Is a List of arrays the expected result?

* How might the query be changed to return just the Parent (i.e. User) object?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 11:13 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is expected. Read the documentation about the FROM and SELECT clauses in HQL.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 11:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
its steve ebersole from austin. :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 11:28 pm 
Newbie

Joined: Mon Apr 19, 2004 7:10 pm
Posts: 7
Thanks, I just reviewed it again. Yes, it is clear that if multiple items are selected, they must be returned via an array mechanism.

What I a wondering is if the query can be reformulated to only return the Parent (i.e. User) objects.

It would seem that some sort of sub-query should work. I tried figuring out a sub-query for this example, but I did not have any luck.

I am hoping that the soon-to-be-published book on Hibernate has a comprehensive explanation of HSQL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 11:30 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It's HQL, and you get what you want with a simple "select u" in your query.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 11:37 pm 
Newbie

Joined: Mon Apr 19, 2004 7:10 pm
Posts: 7
Steve, hi! -- please send me an email to randy.kahle @ variantia.net.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 11:41 pm 
Newbie

Joined: Mon Apr 19, 2004 7:10 pm
Posts: 7
Christian -- very cool.

It worked exactly as you described, and as needed.

Thank you very much.


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