QuerySyntaxException 无效路径 + JPA + Hibernate 5.4

huangapple go评论73阅读模式
英文:

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: &#39;generatedAlias1.package.id&#39; [select count(generatedAlias0) from com.test.Product as generatedAlias0 where ( generatedAlias1.package.id like :param0 )]

Code

CriteriaBuilder cb = session().getCriteriaBuilder();
CriteriaQuery&lt;Product&gt; query = cb.createQuery(Product.class);
Root&lt;Product&gt; entity = query.from(Product.class);
query.where(where_clause);

CriteriaQuery&lt;Long&gt; queryCount = cb.createQuery(Long.class);
Root&lt;Product&gt; 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&lt;Product&gt; root, CriteriaBuilder builder, Parameter param) {
   // returns predicate using root, builder and param you need
   return builder.equal(root.get(&quot;fieldName&quot;), param);
}

And then use it in queries

CriteriaBuilder cb = session().getCriteriaBuilder();
CriteriaQuery&lt;Product&gt; query = cb.createQuery(Product.class);
Root&lt;Product&gt; entity = query.from(Product.class);
query.where(getPredicate(entity, cb, param));

CriteriaBuilder cbCount = session().getCriteriaBuilder();
CriteriaQuery&lt;Long&gt; queryCount = cbCount.createQuery(Long.class);
Root&lt;Product&gt; entityCount = queryCount.from(Product.class);
queryCount.where(getPredicate(entityCount, cbCount, param));

huangapple
  • 本文由 发表于 2020年9月3日 03:53:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63712693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定