冬眠标准多重投影与自定义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

休眠代码

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实体中,这三个是字符串类型

修复了它,以下代码有效。当我们使用变压器时

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

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

因此,您的限制应为:

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

最新更新