selecting only few columns with criteria

2019-08-01 04:12发布

问题:

I have the database query such as ,..

select aaa, bbb, ccc, ddd from ioa_book

as shown it is hitting the table ioa_book and only selecting few columns , the corresponding pojo name is boop .. and now I want to convert it into criteria of hibernate one but it loads all the columns that i do not want , i still want to use criteria and with selected columns itself , please advise how to achieve this..

so I have gone by this way...

 Criteria criteria = session.createCriteria(boop.class);
 List<boop> books = criteria.list();

please advise

回答1:

criteria.setProjection(
    Projections.projectionList()
        .add(Projections.property("aaa"))
        .add(Projections.property("bbb"))
        .add(Projections.property("ccc")) 
    );
List<Object[]> result = criteria.list();

This is documented.