Search code examples
hibernatehibernate-criteria

Hibernate criteria multiple projection tied to a custom POJO


Error

  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 code

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;

In JPA entity these 3 are of string type


Solution

  • Fixed it , The below code works. As we are using Transformers.aliasToBean , each column in the criteria buildup should have an alias

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