Criteria中的setResultTransformer [英] setResultTransformer in Criteria
问题描述
问候,
不使用setProjections()的Criteria查询的默认ResultTransformer将为 ROOT_ENTITY
。
如果我们将Student与Many关系为ManyToMany关系,则查询可能如下所示...
<$ p $会话会话=(会话)getEntityManager()。getDelegate();
Criteria crit = session.createCriteria(Student.class)
.createAlias('departments','department');
该查询将返回重复项。但将 ResultTransformer
设置为...
crit.setResultTransformer(CriteriaSpecification .DISTINCT_ROOT_ENTITY);
现在,当Hibernate收集结果时,结果将不同。 (或者我的意思是unmarshalls?)
如果你不希望Hibernate以 List< Object []>
然后
crit.setResultTransformer(CriteriaSpecification.PROJECTION)
What is the use of setResultTransformer method in criteria API? Can someone explain this with a simple example? I read the javadocs but i am not able to understand them clearly.
Regards,
The default ResultTransformer for a Criteria query which does not use setProjections() will be ROOT_ENTITY
.
If we have Student in a ManyToMany relationship to Department a query might look like this ...
Session session = (Session) getEntityManager().getDelegate();
Criteria crit = session.createCriteria(Student.class)
.createAlias('departments', 'department');
This query will return duplicates. But set the ResultTransformer
as ...
crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
Now the results will be distinct when Hibernate marshalls the results. (Or do I mean unmarshalls?)
If you do not want Hibernate to return the query as a List<Student>
but prefer to handle the results as a List<Object[]>
then
crit.setResultTransformer(CriteriaSpecification.PROJECTION)
这篇关于Criteria中的setResultTransformer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!