This content has been marked as final. Show 2 replies
I usually try forming my queries in JPQL where it is more obvious, or there are more examples, of what needs to be done.
From the JPA specification, section 4.4.4 Path Expressions state
"It is syntactically illegal to compose a path expression from a path expression that evaluates to a collection.
For example, if o designates Order, the path expression o.lineItems.product is illegal
since navigation to lineItems results in a collection. This case should produce an error when the
query string is verified. To handle such a navigation, an identification variable must be declared in the
FROM clause to range over the elements of the lineItems collection. Another path expression must
be used to navigate over each such element in the WHERE clause of the query, as in the following:
SELECT DISTINCT l.product
FROM Order AS o JOIN o.lineItems l"
With Criteria api, the equivalent is:
CriteriaQuery<Product> q = cb.createQuery(Product.class); Root<Order> order = q.from(Order.class); Join<Order, Item> item = order.join(Order_.lineItems); q.select(item.get(Item_.product));