【发布时间】:2021-05-02 15:33:48
【问题描述】:
我有以下java 代码,它使用hibernate 谓词从我的约会MYSQL 表中返回搜索结果。
public List<Appointment> getSearchResults(String client, AppointmentSearchRequest searchRequest, Predicate completePredicate) {
List<Appointment> searchResults = new ArrayList<>();
EntityManager entityManager = null;
try {
entityManager = entityManagement.createEntityManager(client);
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Appointment> query = builder.createQuery(Appointment.class);
Root<Appointment> from = query.from(Appointment.class);
CriteriaQuery<Appointment> selectQuery = query.select(from);
selectQuery = selectQuery.where(completePredicate);
searchResults = entityManager.createQuery(selectQuery).setFirstResult(searchRequest.getIndex()).setMaxResults(searchRequest.getSize()).getResultList();
}catch (Exception e){
//
}
return searchResults;
}
当我运行此代码时,在以下行:
searchResults = entityManager.createQuery(selectQuery).setFirstResult(searchRequest.getIndex()).setMaxResults(searchRequest.getSize()).getResultList();
我收到错误:
17:20:27,730 ERROR [org.hibernate.hql.internal.ast.ErrorCounter] (http-/127.0.0.1:8080-2) Invalid path: 'generatedAlias1.title'
17:20:27,734 ERROR [org.hibernate.hql.internal.ast.ErrorCounter] (http-/127.0.0.1:8080-2) Invalid path: 'generatedAlias1.title': Invalid path: 'generatedAlias1.title'
at org.hibernate.hql.internal.ast.util.LiteralProcessor.lookupConstant(LiteralProcessor.java:119) [hibernate-core-4.2.7.SP1-redhat-3.jar:4.2.7.SP1-redhat-3]
什么可能导致这个错误?
【问题讨论】:
-
您可以尝试更新您的休眠版本并检查一下,这会有所帮助
-
这不起作用
-
最新版本是Hibernate 5.4.x。您尝试了哪个版本?
-
我认为问题在于谓词是由不同的 CriteriaBuilder(不同的别名)创建的
-
是的,问题在于创建谓词的根,您必须确保它与查询中使用的根相同。也许最好的选择是在
getSearchResults中创建 de 谓词并重写方法以另一种方式获取条件。
标签: java mysql hibernate predicate hibernate-criteria