Hibernate Criteria Transformers.aliasToBean没有填充正确的值

时间:2011-09-27 19:19:58

标签: hibernate hibernate-criteria

我试图通过加入我的实体类来创建BO

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如下

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 []时,我可以看到获取的正确值 请指导我......

1 个答案:

答案 0 :(得分:15)

尝试显式别名ProjectionList项以匹配bean中的字段名称,如下所示:

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));