Hibernate Criteria:使用TransferObject进行投影

时间:2013-08-12 18:40:06

标签: hibernate hql criteria jpql

如何将下面的HQL代码转换为Hibernate Criteria查询?

SELECT
    new com.project.to.PersonTO(
        person.firstName,
        person.lastName
    )
FROM
    Person person

1 个答案:

答案 0 :(得分:1)

尝试下面的内容: -

Criteria criteria = session.createCriteria(Person.class);
criteria.setProjection(Projections.projectionList().add(Projections.property("firstName"), "firstName") .add(Projections.property("lastName"), "lastName"))
                    .setResultTransformer(new AliasToBeanResultTransformer(PersonTO.class));