How is it possible to access pre-filtered dependent entities from Associations retrieved via JPA/Hibernate criteria query with restrictions?
<p>I have a provider:user=1:N association modeled with entities, Hibernate/JPA.
Then I want to query a provider/user pair via restrictions on attributes of the dependent entity, like certain values for the attributes of the user, let’s say its id, date of birth, etc.</p>

<p>The logged sql has a proper join and all the attributes of the two entities in the select. I tried it manually, it returns the expected single row.</p>

<p>Thus, on entity level, I expect a single provider entity to be returned with the user list containing the queried user.</p>

<p>Indeed, the corresponding provider entity is returned, but when I then want to access the user via the provider’s user list, it hits the DB a second time and reads all users of the provider totally neglecting my restrictions of the query.</p>

<p>The observed behavior is the same for queries formulated with HQL, Hibernate Criteria (also with @Filter), JPA CriteriaBuilder.</p>

<p>What am I missing here?
Do those restrictions only affect the selection of the root entities (which is provider in my case)?</p>

<p>The problem is sketched on
<a href="https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html" rel="nofollow">https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html</a></p>

<p>Under 5.4 Associations it says:</p>

<p>The kittens collections held by the Cat instances returned by the previous two queries are not pre-filtered by the criteria. If you want to retrieve just the kittens that match the criteria, you must use a ResultTransformer.</p>

<p>Is this thus the intended behavior for this kind of API?
Or is there a convenient possibility to access just the restricted sub set of the dependent entities?</p>

<p>Regards,
Wolfgang </p>