小编典典

休眠条件Transformers.aliasToBean未填充正确的值

hibernate

我正在尝试通过加入实体类来创建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 []时,我可以看到已获取正确的值,请指导我…


阅读 373

收藏
2020-06-20

共1个答案

小编典典

尝试显式地对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));
2020-06-20