1

There are two classes A and B. Class A has a collection of objects of class B in it. Class B has a field X. I'd like to be able to return list of objects of class A that have in their collections objects of type B having some specific value of their property X. Is it even possible?

So far I have tried something like:

Root<A> root = criteriaQuery.from(A.class);
criteriaQuery.select(root);
criteriaQuery.where(root.get("bCollection").get("x").in("value"))

but of course it does not work. How to handle this? Class B is mapped as a nested-component like this:

<set name="bCollection" table="bCollection">
            <key column="a_id" />
            <composite-element
                class="example.B">
                <many-to-one name="x" column="b_id" not-null="false"
                    cascade="none" foreign-key="b_fk" />
            </composite-element>
</set>

I hope someone knows... thank you!

user2902211
  • 185
  • 14

1 Answers1

1

Of course it is possible:

  1. Create the root A (as you did).
  2. Join the root A with B, obtaining a Join B.
  3. Add the where condition, using the obtained Join B.

For examples of the IN clause, google or check this answer.

Community
  • 1
  • 1
V G
  • 18,822
  • 6
  • 51
  • 89
  • OK :)! But how to do the same if that collection of objects of type B is not directly in A, but in some class C inside class A? This class C is mapped as component so it doesn't add up to the hierarchy anything... – user2902211 Jan 09 '14 at 19:30