Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct [英] Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct

查看:116
本文介绍了Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Hibernate我很新。我发现我们可以通过以下两种不同的方式获得不同的结果。任何人都可以告诉我他们之间有什么区别?何时使用其中一种?

  Projections.distinct(Projections.property(id)); 

vs

  criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 


解决方案

虽然名称相似,但用法不同。 p>

I。 Projections.distinct(Projections.property(id));



此语句将被转换为SQL语句。它将被传递给数据库引擎并作为SQL DISTINCT 执行。请参阅:



例如这个例子:

  List results = session.createCriteria(Cat.class)
.setProjection(Projections.projectionList()
.add(Projections.distinct(Projections.property(id)))

.list();

看起来像:

  SELECT DISTINCT(cat_id)FROM cat_table 



II。 criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);



执行 ex-post 。一旦来自数据库引擎的SQL查询被返回,Hibernate会迭代结果集以将其转换为我们实体的列表。



但是它总是需要吗?不,大多数情况下这是不需要的。


唯一的情况是,当我们必须使用它时,如果查询中存在关联 - 加入一对多结束。

因为如果我们有一个 cat 及其两个 小猫 ,这会返回两个行,而 cat 一个

  SELECT cat。*,kitten。* 
FROM cat_table as cat
INNER JOIN kitten_table kitten on kitten.cat_id = cat.cat_id
criteriaQuery

$结尾处的语句是:$ c $ <$ $ $ $ $ $ $ $> b
$ b

  ... // criteria加入root和一些一对多
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)

会产生只有一只猫的列表。

I am pretty new to Hibernate. I found out that we can get distinct result using following two different ways. Could any one tell me what is the difference between them? When to use one over other?

Projections.distinct(Projections.property("id"));

vs

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

解决方案

While similar names, the usage is different.

I. Projections.distinct(Projections.property("id"));

this statement would be translated into SQL Statement. It will be passed to DB Engine and executed as a SQL DISTINCT. See:

so e.g. this example:

List results = session.createCriteria(Cat.class)
    .setProjection( Projections.projectionList()
        .add( Projections.distinct(Projections.property("id")) )
    )
    .list();

would seems like:

SELECT DISTINCT(cat_id) FROM cat_table

II. criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

This statement is executed ex-post. Once the SQL query from DB engine is returned and Hibernate iterates the result set to convert that into list of our entities.

But is it needed always? NO, mostly this is not needed.

The only case, when we MUST to use that, if there is an association in the query - JOINING the one-to-many end.

Because if we do have one cat and its two kittens, this would return two rows, while cat is only one:

SELECT cat.*, kitten.*
FROM cat_table as cat 
  INNER JOIN kitten_table kitten ON kitten.cat_id = cat.cat_id

So, the statement at the end of the criteriaQuery:

... // criteriaQuery joining root and some one-to-many
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)

would result in a list with only one cat.

这篇关于Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆