本文介绍了Hibernate Criteria Transformers.aliasToBean没有填充正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过加入我的实体类来创建BO
I am trying to create BO by joining my entity classes
Criteria criteria = session.createCriteria(Report.class,"r");
criteria
.createAlias("template", "t")
.createAlias("constituents", "rc")
.createAlias("rc.entity", "pe")
.createAlias("pe.model", "m")
.createAlias("pe.scenario", "s")
.setProjection(Projections.projectionList()
.add( Projections.property("r.Id"))
.add( Projections.property("t.Typ"))
.add( Projections.property("pe.bId"))
.add( Projections.property("m.model"))
.add( Projections.property("s.decay"))
).setMaxResults(100)
.addOrder(Order.asc("r.Id"))
.setResultTransformer(Transformers.aliasToBean(BO.class));
我得到100个空BO,即所有属性都为空
我的BO如下
I am getting 100 empty BO i.e. all properties are nullMy BO is as follows
public class BO implements Serializable {
private static final long serialVersionUID = 1L;
private int Id;
private String Typ;
private String bId;
private String model;
private String decay;
Getters and Setters
.....
当我删除aliasToBean行并迭代Object []时,我可以看到正确的值提取
请引导我...
When I remove the line aliasToBean and iterate over Object[] I could see the correct values fetchedPlease guide me...
推荐答案
尝试显式地别名 ProjectionList
项以匹配bean中的字段名称,如下所示:
Try explicitly aliasing the ProjectionList
items to match the field names in the bean, as follows:
Criteria criteria = session.createCriteria(Report.class,"r");
criteria
.createAlias("template", "t")
.createAlias("constituents", "rc")
.createAlias("rc.entity", "pe")
.createAlias("pe.model", "m")
.createAlias("pe.scenario", "s")
.setProjection(Projections.projectionList()
.add( Projections.property("r.Id"), "Id")
.add( Projections.property("t.Typ"), "Typ")
.add( Projections.property("pe.bId"), "bId")
.add( Projections.property("m.model"), "model")
.add( Projections.property("s.decay"), "decay")
).setMaxResults(100)
.addOrder(Order.asc("r.Id"))
.setResultTransformer(Transformers.aliasToBean(BO.class));
这篇关于Hibernate Criteria Transformers.aliasToBean没有填充正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!