8

我正在尝试通过加入我的实体类来创建 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,即所有属性都为 null 我的 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[] 时,我可以看到获取的正确值请指导我...

4

1 回答 1

19

尝试显式地为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));
于 2011-09-27T19:40:20.293 回答