I am trying to write a distinct criteria query, using:

CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<RuleVar> query = builder.createQuery(RuleVar.class); Root<RuleVar> ruleVariableRoot = query.from(RuleVar.class); query.select(ruleVariableRoot.get("foo").<String>get("foo")).distinct(true); 

Based on the example in the javadoc for CriteriaQuery.select()

CriteriaQuery<String> q = cb.createQuery(String.class); Root<Order> order = q.from(Order.class); q.select(order.get("shippingAddress").<String>get("state")); 

However, this gives me an error:

The method select(Selection<? extends RuleVar>) in the type CriteriaQuery<RuleVar> is not applicable for the arguments (Path<String>) 

Can someone please point out what I am doing wrong? Or how to get a Selection object from a Path?

1 Answer

I got it. The problem was my CriteraQuery needed to be of type String. This works:

CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<String> query = builder.createQuery(String.class); Root<RuleVar> ruleVariableRoot = query.from(RuleVar.class); query.select(ruleVariableRoot.get(RuleVar_.varType)).distinct(true); 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.