Query a Hibernate many-to-many association
Posted
by Perry Hoekstra
on Stack Overflow
See other posts from Stack Overflow
or by Perry Hoekstra
Published on 2010-05-17T16:30:49Z
Indexed on
2010/05/17
17:20 UTC
Read the original article
Hit count: 240
In Hibernate HQL, how would you query through a many-to-many association. If I have a Company with multiple ProductLines and other companies can offer these same product lines, I have a Company entity, a ProductLine entity and an association table CompanyProductLine. In SQL, I can get what I need like this:
select * from company c where c.companyId in (select companyId from companyProductLine cpl, productline pl where cpl.productLineId = pl.productLineId and pl.name= 'some value');
My problem sees to lie with the association I defined in the Company.hbm.xml file:
<set name="productLines"
cascade="save-update"
table="CompanyProductLine">
<key column="companyId"/>
<many-to-many class="com.foo.ProductLine" column="productLineId" />
</set>
Any HQL I seem to come up with will throw a: 'expecting 'elements' or 'indices"' Hibernate exception.
Thoughts on what the proper HQL would be?
© Stack Overflow or respective owner