views:

143

answers:

1

Is it possible to implement the following query using Criteria API?

select order from ORDER as order,ITEM as item 
where item.itemID like 'ITM_01' and item in elements(order.items)
A: 

To answer the question in the title - no, there's no direct equivalent for elements() in Criteria API.

However, your query's use of elements() is superfluous. It can instead be more simply rewritten as

select order from ORDER as order
  where order.items.itemID like 'ITM_01'

Equivalent criteria would have to use nested criteria instance to access collection:

session.createCriteria(Order.class)
 .createCriteria("items")
 .add(Restrictions.like("itemID", "ITM_01"));

Another alternative is to use aliases:

session.createCriteria(Order.class)
 .createAlias("items", "item")
 .add(Restrictions.like("item.itemID", "ITM_01"));

Note that you don't need to use LIKE for fixed value, you can use simple equality instead.

ChssPly76