英文:
QuerySyntaxException Invalid path + JPA + Hibernate 5.4
问题
我正在尝试根据 CriteriaQuery 获取基于条件的总行数,但出现了异常
org.hibernate.hql.internal.ast.QuerySyntaxException: 无效路径: 'generatedAlias1.package.id' [select count(generatedAlias0) from com.test.Product as generatedAlias0 where ( generatedAlias1.package.id like :param0 )]
**代码**
CriteriaBuilder cb = session().getCriteriaBuilder();
CriteriaQuery<Product> query = cb.createQuery(Product.class);
Root<Product> entity = query.from(Product.class);
query.where(where_clause);
CriteriaQuery<Long> queryCount = cb.createQuery(Long.class);
Root<Product> entity = queryCount.from(query.getResultType());
queryCount.where(query.getRestriction()) -- 这是问题出现的地方
**实体**
class Product{
Package package;
int quantity;
/// getter setter 方法
}
class Package{
String id;
String name;
String type
/// getter setter 方法
}
映射使用 hbm xml 文件完成。
你能告诉我如何解决这个问题吗?
英文:
I am trying to get the total row count based on the CriteriaQuery but got an exception
org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'generatedAlias1.package.id' [select count(generatedAlias0) from com.test.Product as generatedAlias0 where ( generatedAlias1.package.id like :param0 )]
Code
CriteriaBuilder cb = session().getCriteriaBuilder();
CriteriaQuery<Product> query = cb.createQuery(Product.class);
Root<Product> entity = query.from(Product.class);
query.where(where_clause);
CriteriaQuery<Long> queryCount = cb.createQuery(Long.class);
Root<Product> entity = queryCount.from(query.getResultType());
queryCount.where(query.getRestriction()) -- this is where the problem is creating
Entity
class Product{
Package package;
int quantity;
/// getter setter method
}
class Package{
String id;
String name;
String type
/// getter setter method
}
mapping is done using hbm xml file.
can you please let me know how to fix it ?
答案1
得分: 10
你有两个不同的查询。因此,你不能对它们都使用相同的谓词,因为它们具有不同的根节点。
generatedAlias1
是另一个查询中 Product
的别名。
为了使谓词可重用,你应该创建一个方法来返回谓词:
Predicate getPredicate(Root<Product> root, CriteriaBuilder builder, Parameter param) {
// 使用 root、builder 和 param 返回谓词
return builder.equal(root.get("fieldName"), param);
}
然后在查询中使用它:
CriteriaBuilder cb = session().getCriteriaBuilder();
CriteriaQuery<Product> query = cb.createQuery(Product.class);
Root<Product> entity = query.from(Product.class);
query.where(getPredicate(entity, cb, param));
CriteriaBuilder cbCount = session().getCriteriaBuilder();
CriteriaQuery<Long> queryCount = cbCount.createQuery(Long.class);
Root<Product> entityCount = queryCount.from(Product.class);
queryCount.where(getPredicate(entityCount, cbCount, param));
英文:
You have two different queries. So you can't use the same predicate for both of them, because they have different roots.
> org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path:
> 'generatedAlias1.package.id' [select count(generatedAlias0) from
> com.test.Product as generatedAlias0 where ( generatedAlias1.package.id
> like :param0 )]
generatedAlias1
is alias for Product
from another query
To make the predicate reusable you should create method returns predicate
Predicate getPredicate(Root<Product> root, CriteriaBuilder builder, Parameter param) {
// returns predicate using root, builder and param you need
return builder.equal(root.get("fieldName"), param);
}
And then use it in queries
CriteriaBuilder cb = session().getCriteriaBuilder();
CriteriaQuery<Product> query = cb.createQuery(Product.class);
Root<Product> entity = query.from(Product.class);
query.where(getPredicate(entity, cb, param));
CriteriaBuilder cbCount = session().getCriteriaBuilder();
CriteriaQuery<Long> queryCount = cbCount.createQuery(Long.class);
Root<Product> entityCount = queryCount.from(Product.class);
queryCount.where(getPredicate(entityCount, cbCount, param));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论