Hibernate version: 3.1.3
I've got a typical many-to-many realtionship between EntityA and EntityB. EntityA has a property that is a Set of EntityBs, like so:
Code:
<set name="entityBs" fetch="join">
<key column="B_ID" not-null="true" />
<many-to-many column="B_ID" class="EntityB" />
</set>
I want to query to find all EntityA instances that contain a particular EntityB in their set. I've got it working with the following HQL query:
Code:
from EntityA where :bID in elements(entityBs)
and I call setLong("bID", idOfTheBInstanceImLookingFor);
Although this appears to be working, it seems to me that there would be a more "clean" way to structure such a query. I'd like to use the Criteria API if possible, but I am not a relational thinker and my perusal of the refernce docs didn't make it obvious how to do so.
The HQL I have doesn't seem all that straightforward, especially since I will eventually have to query against several different m-t-m realtionships that EntityA has Sets for (meaning my HQL would end up with a series of ORs in the where clause - yuck).
Can anyone offer a better way to structure such a query (possibly using Criteria)? Seems like it would be a common kind of requirement.
Thanks in advance,
Eric