简体   繁体   English

在Hibernate 5中,CriteriaQuery相当于Criteria的限制和预测是什么?

[英]In Hibernate 5, what's the CriteriaQuery equivalent of Criteria's restriction and projection?

Before Hibernate 5 deprecated the Criteria class, you could add restrictions to a Criteria to act as a constraint, and projections to act as select statements, like so 在Hibernate 5弃用Criteria类之前,您可以向Criteria添加限制以充当约束,并将投影添加为select语句,如此

Criteria criteria = session.createCriteria(T.class)
    .add(Restrictions.or(Restrictions.eq(property, constraintValue)
    .set(Projection(Projections.projectionList()
    .add(Projections.property(selectValue)));

But, since you now need to use CriteriaQuery like so, 但是,既然你现在需要像这样使用CriteriaQuery,

 CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
 CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(T.class);
 Root<T> root = criteriaQuery.from(T.class);
 criteriaQuery.select(root);

 Query<T> query = session.createQuery(criteriaQuery);

However I have been unable to figure out how to add certain things which are required in SQL statements, mainly because searching for documentation tends to wind up on documentation about Criteria, due to the naming similarity. 但是我一直无法弄清楚如何添加SQL语句中所需的某些东西,主要是因为搜索文档往往会因为命名相似性而最终出现关于Criteria的文档。

So, how can I recreate a simple query, like the one below, using CriteriaQuery? 那么,如何使用CriteriaQuery重新创建一个简单的查询,如下所示呢?

SELECT selectValue  
FROM tables.T  
WHERE property = constraintValue

Source. 资源。

Has multiple examples, but turns out that the simple select statement we were trying to recreate can be done like so: 有多个示例,但事实证明我们尝试重新创建的简单select语句可以像这样完成:

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<SELECTVALUETYPE> criteriaQuery = criteriaBuilder.createQuery(SELECTVALUETYPE.class);
Root<PARENTCLASS> root = criteriaQuery.from(PARENTCLASS.class);
criteriaQuery.select(root);
criteriaQuery.where(criteriaBuilder.equal(root.get(property), constraintValue));

Query<SELECTVALUETYPE> query = session.createQuery(criteriaQuery);

Note that this is a generic answer, and won't actually run. 请注意,这是一个通用答案,实际上不会运行。 The reason being, SELECTVALUETYPE needs to be replaced with the data type of selectValue. 原因是,SELECTVALUETYPE需要替换为selectValue的数据类型。
For example, CriteriaQuery might become: 例如,CriteriaQuery可能会变为:

  • String selectValue -> CriteriaQuery String selectValue - > CriteriaQuery
  • T selectValue -> CriteriaQuery T selectValue - > CriteriaQuery

Therefore, a working example for the statement 因此,该声明的工作示例

Select name  
From Users  
Where ID = 1

Could be expressed with the following block 可以用以下块表示

int ID = 1;
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<String> criteriaQuery = criteriaBuilder.createQuery(String.class);
Root<User> root = criteriaQuery.from(User.class);
criteriaQuery.select(root.get("name");
criteriaQuery.where(criteriaBuilder.equal(root.get("ID"), ID));

Query<String> query = session.createQuery(criteriaQuery);

List<String>results = query.getResultList();
for(String name : results){
    System.out.println("Name: " + name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM