Hibernate标准将多个投影绑定到自定义POJO

时间:2017-03-05 07:54:44

标签: hibernate hibernate-criteria

错误

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

Custorm POJO

public class AwardsInSession extends BaseModel{
   private String uid;

   private String path;

   private String name;

在JPA实体中,这3个是字符串类型

2 个答案:

答案 0 :(得分: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));

答案 1 :(得分:0)

在您的Award实体中,您将deleted属性字段定义为布尔值。

因此,你的限制应该是:

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