绑定到自定义 POJO 的 Hibernate 标准多重投影

Hibernate criteria multiple projection tied to a custom 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();

自定义 POJO

public class AwardsInSession extends BaseModel{
   private String uid;

   private String path;

   private String name;

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

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

因此您的限制应该是:

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

已修复,下面的代码有效。当我们使用 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));