cz_Nesh. sorry for my first answer. I am reading the Hibernate api and reading some Hibernate source code that I find. if you use this code
session.createCriteria(EmpUserImpl.class).list();
it will return List EmpUserImpl. if you use this code
criteria.setProjection(Projections.projectionList() .add(Projections.groupProperty("company").as("company")) .add(Projections.property("name").as("name")) .add(Projections.property("company").as("company"))); List list = criteria.list();
it will return List, not List EmpUserImpl, why? I see the criterion of the parent class CriteriaSpecification, which I find.
public interface CriteriaSpecification { public static final String ROOT_ALIAS = "this"; public static final ResultTransformer ALIAS_TO_ENTITY_MAP = AliasToEntityMapResultTransformer.INSTANCE; public static final ResultTransformer ROOT_ENTITY = RootEntityResultTransformer.INSTANCE; public static final ResultTransformer DISTINCT_ROOT_ENTITY = DistinctRootEntityResultTransformer.INSTANCE; public static final ResultTransformer PROJECTION = PassThroughResultTransformer.INSTANCE; @Deprecated public static final int INNER_JOIN = JoinType.INNER_JOIN.getJoinTypeValue(); @Deprecated public static final int FULL_JOIN = JoinType.FULL_JOIN.getJoinTypeValue(); @Deprecated public static final int LEFT_JOIN = JoinType.LEFT_OUTER_JOIN.getJoinTypeValue();
}
Can you see the public static final ResultTransformer PROJECTION? he says that this result transformer is selected implicitly, calling setProjection () is average, when you use the .setProjection criteria, the result will not be displayed in EmpUserImpl because the ResultTransformer changes to “PROJECTION” with “ROOT_ENTITY.” it will package using Projection (e.g. select name, oid ..). therefore, if you want to return List EmpUserImpl, you need to set Projections.property ("name"). as ("name"). (if you only need a name with a name). this is my code.
Criteria criteria = session.createCriteria(EmpUserImpl.class); criteria.setProjection(Projections.projectionList() .add(Projections.groupProperty("company").as("company")) .add(Projections.property("name").as("name")) .add(Projections.property("company").as("company"))); criteria.setResultTransformer(Transformers.aliasToBean(EmpUserImpl.class)); List<EmpUserImpl> list = criteria.list(); for (EmpUserImpl empUserImpl : list) { System.out.println(empUserImpl.getName()); }
he can work. I hope this can help you.
source share