Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Criteria Transformers.aliasToBean is not populating correct values

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

I am getting 100 empty BO i.e. all properties are null My 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

.....

When I remove the line aliasToBean and iterate over Object[] I could see the correct values fetched Please guide me...

like image 467
Rohit Avatar asked Sep 27 '11 19:09

Rohit


Video Answer


1 Answers

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));
like image 76
atrain Avatar answered Oct 10 '22 10:10

atrain