简体   繁体   English

JPA Criteria Query不同

[英]JPA Criteria Query distinct

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.select()的javadoc中的示例

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? 或者如何从Path获取Selection对象?

I got it. 我知道了。 The problem was my CriteraQuery needed to be of type String. 问题是我的CriteraQuery需要是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM