-->
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: HQL question
PostPosted: Mon Jun 21, 2004 12:09 am 
Beginner
Beginner

Joined: Wed Oct 01, 2003 11:01 pm
Posts: 23
If I have a class Foo that has a collection of Bars with a mapping like:
Code:
<class name="Foo" table="Foo">
  <id name="id" column="fooId" />
  <set name="bars" table="FooBarAssoc">
    <key column="fooId" />
    <many-to-many column="barId" class="Bar" />
  </set>
  <property name="owner" column="owner" />
</class>

<class name="Bar" table="Bar">
  <id name="id" column="barId" />
  <property name="name" column="name" />
</class>


What would the HQL equivalent of the following SQL?
Code:
select bar.* from foo, bar, foobarAssoc
where foo.owner='me'
and foobarAssoc.fooId = foo.fooId
and bar.barId = foobarAssoc.barId


The best I can come up with is:
Code:
select bar from bar, foo
where foo.owner='me' and bar in elements(foo.bars)


Unfortunately, this produces a subselect. Is there a way to avoid the subselect?

In general, when using many-to-many mappings, is there a way to use the association table directly in HQL queries?


Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 21, 2004 3:55 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
1. consider setting a bidirectional many-to-many
2.
Code:
select bar from Foo foo join foo.bars bar where foo.name=..
[/quote]

_________________
Emmanuel


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.