Java 休眠条件:dto作为实体

Java 休眠条件:dto作为实体,java,hibernate,criteria,Java,Hibernate,Criteria,我有一个使用Hibernate条件构建的查询(我只向您显示“主要”部分): 所以我终于可以做到: List<User> users = criteria.list(); 返回null。因此,Transformers.aliasToBean通过投影仅接收“id”的创建的实体(如“DTO”)基本上与get/load/etc加载的普通实体不同 有没有办法让这些数据作为实体“工作”?似乎在projectionList中传递参数是错误的,您需要像这样修改projectionList Crit

我有一个使用Hibernate条件构建的查询(我只向您显示“主要”部分):

所以我终于可以做到:

List<User> users = criteria.list();
返回null。因此,Transformers.aliasToBean通过投影仅接收“id”的创建的实体(如“DTO”)基本上与get/load/etc加载的普通实体不同


有没有办法让这些数据作为实体“工作”?

似乎在projectionList中传递参数是错误的,您需要像这样修改projectionList

Criteria criteria = sessionProvider.get().createCriteria(User.class);
//Add other stuff to the query like joins, group-bys, order-bys etc.

//In the projection list add the columns name which are mapped with the entity
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("id"));
projectionList.add(Projections.property("firstName"));
criteria.setProjection(projectionList);

List<User> results = criteria.list();

//output results
for(User user : results) {
    user.getFirstName();        
}
Criteria=sessionProvider.get().createCriteria(User.class);
//向查询中添加其他内容,如联接、分组依据、订单依据等。
//在“投影”列表中,添加与实体映射的列名称
ProjectionList ProjectionList=Projections.ProjectionList();
projectionList.add(Projections.property(“id”));
projectionList.add(Projections.property(“firstName”);
标准:setProjection(投影列表);
列表结果=标准。列表();
//输出结果
for(用户:结果){
user.getFirstName();
}

不要设置
投影
结果转换器
会话提供程序.get().createCriteria(User.class).list()
。如果我不使用它们,它将返回一个对象[]与groupbys相关的值。在这种情况下,我认为您需要在那里发布更多的查询,以便我们能够计算出它实际返回的内容。。。
users.get(0).getFirstName();
Criteria criteria = sessionProvider.get().createCriteria(User.class);
//Add other stuff to the query like joins, group-bys, order-bys etc.

//In the projection list add the columns name which are mapped with the entity
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("id"));
projectionList.add(Projections.property("firstName"));
criteria.setProjection(projectionList);

List<User> results = criteria.list();

//output results
for(User user : results) {
    user.getFirstName();        
}