-->
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: Named query with nested list
PostPosted: Thu Apr 17, 2014 2:10 pm 
Newbie

Joined: Thu Apr 17, 2014 1:21 pm
Posts: 5
Hello all,
I have two classes, Owner and Pet.
A Owner has a Pet, but a Pet does not know about it's Owner.

So I am attempting to write a named query in order to find an Owner when I have a Pet.
My thought is that I can take the petId and look through all Owners, look at the Owner's PetList, check if the PetList contains a Pet with the PetId I am looking for, return that Owner.

Query so far...(not correct)
Code:
    <query name="Owner.findByPet">
        <![CDATA[from Owner o where o.petList contains Pet p where p.id = ?]]>
    </query>


Any hints would be appreciated!


Top
 Profile  
 
 Post subject: Re: Named query with nested list
PostPosted: Thu Apr 17, 2014 3:02 pm 
Newbie

Joined: Mon Apr 07, 2014 10:20 am
Posts: 12
Quote:
A Owner has a Pet, but a Pet does not know about it's Owner.


I assume you mean in your entities a Pet object doesn't hold onto its Owner object. On your database table (assuming a standard one-to-many mapping for owner to pet) your pet table should indicate the owner_id for that pet.

Anyway, if my above assumption is correct you can do this:

Code:
SELECT o FROM Owner o
    JOIN Pet p ON o.id = p.ownerId
   WHERE p.id = :petId

-- Or... if all you care about is retrieving a results set with the owner_id then simply...
SELECT p.owner_id FROM pet p WHERE p.id = :petId


Top
 Profile  
 
 Post subject: Re: Named query with nested list
PostPosted: Thu Apr 17, 2014 7:33 pm 
Newbie

Joined: Thu Apr 17, 2014 1:21 pm
Posts: 5
debunked, thank you for the reply, your first solution seems to be a correct solution. It passes in my DAO test case but it will take other tests to determine if it is working properly.

Quote:
I assume you mean in your entities a Pet object doesn't hold onto its Owner object. On your database table (assuming a standard one-to-many mapping for owner to pet) your pet table should indicate the owner_id for that pet.


Yes your assumptions were correct. It is a one-to-many mapping and the Pet object doesn't hold it's Owner object.
I will have to educate myself on the join parameter since that was my missing link.


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.