When using similar names, usage is different.
I. Projections.distinct(Projections.property("id"));
this statement will be translated into SQL Statement. It will be passed to DB Engine and executed as SQL DISTINCT . Cm:
those. this example:
List results = session.createCriteria(Cat.class) .setProjection( Projections.projectionList() .add( Projections.distinct(Projections.property("id")) ) ) .list();
will look like this:
SELECT DISTINCT(cat_id) FROM cat_table
II. criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
This instruction is executed ex-post . After the SQL query returns from the database engine, Hibernate iterates the result set to convert it to a list of our objects.
But is it always necessary? NO, basically it is not required.
The only time we MUST use this if there is an association in the request is the ENTRY at the end of one-to-many .
Since if we have one cat and two kittens , this will return two lines, and cat only :
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)
will result in a list with only one cat.
Radim KΓΆhler Aug 28 '14 at 5:32 2014-08-28 05:32
source share