JPA/Criteria API - 类似 &平等问题
我正在尝试在我的新项目中使用 Criteria API:
public List<Employee> findEmps(String name) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
Root<Employee> emp = c.from(Employee.class);
c.select(emp);
c.distinct(emp);
List<Predicate> criteria = new ArrayList<Predicate>();
if (name != null) {
ParameterExpression<String> p = cb.parameter(String.class, "name");
criteria.add(cb.equal(emp.get("name"), p));
}
/* ... */
if (criteria.size() == 0) {
throw new RuntimeException("no criteria");
} else if (criteria.size() == 1) {
c.where(criteria.get(0));
} else {
c.where(cb.and(criteria.toArray(new Predicate[0])));
}
TypedQuery<Employee> q = em.createQuery(c);
if (name != null) {
q.setParameter("name", name);
}
/* ... */
return q.getResultList();
}
现在,当我将此行更改
criteria.add(cb.equal(emp.get("name"), p));
为:时,
criteria.add(cb.like(emp.get("name"), p));
我收到一条错误消息:
CriteriaBuilder类型中的(Expression, Expression)这样的方法不是>适用于参数(Path、ParameterExpression)
有什么问题?
I'm trying to use Criteria API in my new project:
public List<Employee> findEmps(String name) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
Root<Employee> emp = c.from(Employee.class);
c.select(emp);
c.distinct(emp);
List<Predicate> criteria = new ArrayList<Predicate>();
if (name != null) {
ParameterExpression<String> p = cb.parameter(String.class, "name");
criteria.add(cb.equal(emp.get("name"), p));
}
/* ... */
if (criteria.size() == 0) {
throw new RuntimeException("no criteria");
} else if (criteria.size() == 1) {
c.where(criteria.get(0));
} else {
c.where(cb.and(criteria.toArray(new Predicate[0])));
}
TypedQuery<Employee> q = em.createQuery(c);
if (name != null) {
q.setParameter("name", name);
}
/* ... */
return q.getResultList();
}
Now when I change this line:
criteria.add(cb.equal(emp.get("name"), p));
to:
criteria.add(cb.like(emp.get("name"), p));
I get an error saying:
The method like(Expression, Expression) in the type CriteriaBuilder is not > applicable for the arguments (Path, ParameterExpression)
What's the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许您需要
,因为
like()
的第一个参数是Expression
,而不是Expression
如equal( )
。另一种方法是启用静态元模型的生成(请参阅 JPA 实现的文档)并使用类型安全的 Criteria API:(
请注意,您无法从
em.getMetamodel()
获取静态元模型,您需要通过外部工具生成它)。Perhaps you need
because first argument of
like()
isExpression<String>
, notExpression<?>
as inequal()
.Another approach is to enable generation of the static metamodel (see docs of your JPA implementation) and use typesafe Criteria API:
(Note that you can't get static metamodel from
em.getMetamodel()
, you need to generate it by external tools).更好:谓词(不是
ParameterExpression
),如下所示:Better: predicate (not
ParameterExpression
), like this :它将与少量添加
.as(String.class)
一起使用:It will work with a small addition of
.as(String.class)
:使用 :
Use :
我尝试了所有的建议,虽然它没有给出错误,但如果我没有输入所有预期的文本,它不会恢复任何内容,最后 user3077341 提出的内容对我有用,但使用我使用“%”的变体而不是“%”,类似的效果很完美。
I tried all the proposals and although it did not give an error, it did not recover anything if I did not put all the expected text, in the end what was proposed by user3077341 worked for me but with the variant that I use '%' instead of "%", the like worked perfect.