-->
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.  [ 2 posts ] 
Author Message
 Post subject: Searching sub-lists of entities
PostPosted: Wed Jul 29, 2009 8:07 pm 
Beginner
Beginner

Joined: Thu Jan 29, 2009 1:24 am
Posts: 20
I'm trying to figure out the Hibernate way of querying a list of entities that may contain an entity within it. Since that's probably confusing i'll just explain the situation more clearly.

I have an Entity A class which has a OneToMany relationship with Entity B. Now given a specific Entity B I want to be able to find all Entity A objects which contain the Entity B object in their list.

In SQL this would look like:
Code:
select t1.* from entityA as t1 inner join entityA_entityB_map as t2 on t1.id = t2.entityA_id where t2.entityB_id=?;

or
Code:
select t1.* from entityA as t1 where t1.id in (select t2.entityA_id from entityA_entityB_map as t2 where entityB_id=?);


So what would the JPA/HQL equivalent look like? Or should I use something else?

Thanks for any help!


Top
 Profile  
 
 Post subject: Re: Searching sub-lists of entities
PostPosted: Thu Jul 30, 2009 6:44 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
Wouldn't that be something like

from EntitiyA as a left join a.children as b where b = :referenceEntityB

I haven't tested it, but i believe this one will create an outer join which again
will return the elements that has B in it.

If you want the EntityA to come back with its children collection initialised, add "fetch" after "left join":

from EntitiyA as a left join fetch a.children as b where b = :referenceEntityB


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