Search code examples
javasqlhibernatehibernate-criteria

how can map the hibernate criteria resultset to the java class?


i have a entity with many field,i need to get some of them with a query and save in a java class? i know how can set projections and get Specified field ,but i don't know how to map resultset to java class.

this is my query:

    Criteria criteria=session.createCriteria(EmployeeInfo.class);
    Criterion condition1= Restrictions.eq("employeeTO.id",id);  
    criteria.add(condition1);
    criteria.setProjection(Projections.projectionList()
            .add(Projections.property("name"))
            .add(Projections.property("birthDay")));

i need to save result to list of below class

public class SubEmployeeInfo{
    String name;
    int brithDay;
}

How can I save critria.list() into a List<SubEmployeeInfo>?

i have used below code but all field of returned SubEmployeeInfo object is null

criteria.setResultTransformer(Transformers.aliasToBean(HokmKargoziniDTO.class));
SubEmployeeInfoList = (List<SubEmployeeInfo>) criteria.list();

Solution

  • You may use setResultTransformer method while building criteria. ResultTransformer objects can create you marshaling results to any desired format.

    Example:

    criteria.setResultTransformer(Transformers.aliasToBean(SubEmployeeInfo.class));