Search code examples
hibernatecriteriaprojectionhibernate-criteriaresulttransformer

Hibernate : Projection to boolean on a String property


I use a Criteria request using a Projections list to return a custom DTO. For now I have two projections on properties :

criteria.setProjection(
   Projections.projectionList
      .add(Projections.property("Employee.id"), "id")
      .add(Projections.property("Employee.name"), "name"))
   .setResultTransformer(Transformers.aliasToBean(EmployeeDto.class));

In my EmployeeDto, i have a boolean property "hasPicture". This information is a Nullable String column (the name if the picture actually) on my Employee table.

I do not care about the name itself, I would like to add a new projection that does the following:

PictureName != null --> dto.hasPicture = true

PictureName == null --> dto.hasPicture = false

Is that possible? How?


Solution

  • You could do this check within your hasPicture function.

      public Boolean hasPicuture(){
         if (this.picture == null)
          return false 
        return this.picture
         }
    

    Another way would be to write your own Boolean type and use it in your mappings.