setResultTransformer in Criteria

跟風遠走 提交于 2019-11-30 08:15:37

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)
Appesh

Simply: when you are doing the Criteria: you want the resultset to be into particular object the you can use it like:eg:

 .setResultTransformer(Transformers.aliasToBean(Employee.class));

Employee should match the ResultSet Entity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!