2017-03-05 67 views
1

錯誤休眠標準的多個投影綁在自定義POJO

Hibernate: select this_.UID as y0_, this_.PATH as y1_, this_.NAME as y2_ from AWARD this_ where this_.DELETED=? 
    java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Boolean 

Hibernate代碼

criteria.add(Restrictions.eq("deleted", 0)); 
     criteria.setProjection(Projections.projectionList().add(Projections.property("uid")) 
       .add(Projections.property("path")).add(Projections.property("name"))). 
     setResultTransformer(Transformers.aliasToBean(AwardsInSession.class)); 

     List<AwardsInSession> la = criteria.list(); 

度身訂做POJO

public class AwardsInSession extends BaseModel{ 
    private String uid; 

    private String path; 

    private String name; 

在JPA實體這些3是字符串類型

回答

1

修復它,下面的代碼工作。由於我們使用Transformers.aliasToBean,在標準累積每列應該有一個別名

criteria 
      .add(Restrictions.eq("deleted", Boolean.FALSE)) 
      .add(Restrictions.eq("orgId.id", orgId)) 
      .setProjection(Projections.projectionList() 
        .add(Projections.alias(Projections.property("path"), "path")) 
        .add(Projections.alias(Projections.property("uid"), "uid")) 
        .add(Projections.alias(Projections.property("name"), "name"))) 
      .setResultTransformer(Transformers.aliasToBean(AwardsInSession.class)); 
+0

是的,這是一個很好的地方 –

0

在你的胡ard實體,您將deleted屬性字段定義爲布爾值。

因此您的限制應該是:

criteria.add(Restrictions.eq("deleted", Boolean.FALSE)); 
+0

感謝的人擺脫了異常,但我無法得到的值列表 alowsarwar

+0

什麼異常? –

+0

沒有例外,但是所有的獎勵值都是null。該查詢應該返回1行 – alowsarwar