0

Is it possible to add a nested SELECT IN statement with the Hibernate 5.3+ CriteriaBuilder?

I want to create the following SQL with the criteria builder:

select * from ORDERS where sender='ABC' and receiver='DEF' and amount='500' and id IN (SELECT id FROM cc4.best_orders)

Criterion definition (cc4.best_orders is a simple sql view):

public class BestOrders implements Criterion {

private static final String SQL_CRITERIA =
            ".id IN (SELECT id FROM cc4.best_orders) ";

    @Override
    public TypedValue[] getTypedValues(Criteria criteria,
            CriteriaQuery criteriaQuery) {
        return new TypedValue[] {};
    }

    @Override
    public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) {
        return criteriaQuery.getSQLAlias(criteria) + SQL_CRITERIA;
    }

}

Client call:


//...Old deprecated working way

Criteria crit = getSession().createCriteria(Order.class);
crit.add(getRestriction("sender.id", sender));
crit.add(getRestriction("receiver.id", receiver));
crit.add(getRestriction("amount.id", dataType));
crit.add(new BestOrders());
crit.setReadOnly(true);
Order order = (Order) crit.uniqueResult();

//...New Hibernate 5.3+ way 

CriteriaBuilder cb = getSession().getCriteriaBuilder();
CriteriaQuery<Order> cr = cb.createQuery(Order.class);
Root<Order> root = cr.from(Order.class);
cr.select(root).where(
   cb.and(
         getEqualPredicate(root, cb, "sender.id", sender),
         getEqualPredicate(root, cb, "receiver.id", receiver),
         getEqualPredicate(root, cb, "amount.id", dataType)

        //new BestOrders()   ----> how to add 'SELECT IN' here ?!

        )
   );
Query<Order> query = getSession().createQuery(cr);
Order order = query.uniqueResult();

Laurel
  • 5,965
  • 14
  • 31
  • 57
YaMann
  • 11
  • 2
  • I found one of the possible way..it's to obtain an EntityManager from the Hibernate Session and run raw SQL query through the it. But it's really a hardcoded and not portable solution. – YaMann Nov 13 '19 at 15:14

1 Answers1

0

After eating all possible JPA manuals I found the proper way (using JPA subquery featture):


CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();

CriteriaQuery<Order> orderCriteriaQuery = cb.createQuery(Order.class);
Root<Order> orderRoot = orderCriteriaQuery.from(Order.class);

Subquery<String> bestOrdersSubQuery = orderCriteriaQuery.subquery(String.class);
Root<BestOrders> bestOrdersRoot = bestOrdersSubQuery.from(BestOrders.class);
bestOrdersSubQuery.select(bestOrdersRoot.get("id"));

orderCriteriaQuery.select(orderRoot).where(
        cb.and(
            getEqualPredicate(cb, orderRoot.get("sender").get("id"), sender),
            getEqualPredicate(cb, orderRoot.get("receiver").get("id"), receiver),
            getEqualPredicate(cb, orderRoot.get("amount").get("id"), dataType),
            orderRoot.get("id").in(
                    bestOrdersSubQuery
            )
        )
);
TypedQuery<Order> query = getEntityManager().createQuery(orderCriteriaQuery);
Order orderConfig = query.getSingleResult();

Good luck!

YaMann
  • 11
  • 2