如何在JPA 2.0中使用类似条件 - Hibernate for Criteria Builder

时间:2013-05-10 17:37:29

标签: hibernate jpa jpa-2.0

我在DAO课程中有以下过滤条件

ParameterExpression<Long> pexp = criteriaBuilder.parameter(Long.class,"empNo");

criteria.add(criteriaBuilder.equal(emp.get(Employee_.empNo),pexp));

正如你可以看到我的empNo类型为Long,如果我想使用而不是代码,我该如何使用 等于?我试过

criteria.add(criteriaBuilder.like(emp.<Long>get(Employee_.empNo), pexp)); // gives compilation errors

修改1

我也尝试了以下内容

criteria.add(criteriaBuilder.like(emp.<String>get(Employee_.empNo.toString()),
pexp.toString()));

and this as well

criteria.add(criteriaBuilder.like(emp.<String>get(String.valueOf(Employee_.empNo)),
String.valueOf(pexp)));

然而,当我跑步时,我得到以下异常

Unable to resolve attribute [org.hibernate.ejb.metamodel.SingularAttributeImpl$Identifier@a21c8
a] against path
java.lang.IllegalArgumentException: Unable to resolve attribute [org.hibernate.ejb.metamodel.Singula
rAttributeImpl$Identifier@a21c8a] against path
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:116)
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:221)
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:192)

0 个答案:

没有答案