Uploaded image for project: 'Spring Data JPA'
  1. Spring Data JPA
  2. DATAJPA-1713

Projection fails when using subquery

    XMLWordPrintable

    Details

      Description

      When using a subquery in query defined with @Query, JPA projection fails.
      Return type is not of projection class but

      org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap

      Example :

      @Entity
      public class Employee {
         @Id
         @GeneratedValue(strategy=GenerationType.AUTO)
         private Long id;
         private String firstName;
         private String lastName;
         private Integer salary;
      
         // Constructors, getters and setters
      }
      

      Projection class :

      public interface EmployeeProjection {
      
          public Long getId();
      
          public String getFirstName();
       
          //@Value("#{target.firstName + ' ' + target.lastName}") 
          //String getFullName();
      }
      

      Repo :

      public interface EmployeeRepository extends CrudRepository<Employee, Long> {
         @Query("select c from Employee c where c.salary >= "
                //+ "500"
                + "(select avg(salary) from Employee )"
         )
         List<EmployeeProjection> findProjections();
      }
      

      When using condition "c.salary >= 500", method returns a correct result, i.e. a List<EmployeeProjection>.

      But when using a subquery "c.salary >= (select avg(salary) from Employee)", method returns a list of AbstractJpaQuery$TupleConverter$TupleBackedMap.

      On this object, calling any getter returns null.
      Using @Value (open projection) throws an exception.

      Example sources are attached.
      Thanks in advance.

       

        Attachments

        1. demo.zip
          5 kB
        2. demo2.zip
          6 kB

          Activity

            People

            Assignee:
            schauder Jens Schauder
            Reporter:
            z8manu8z Manu VIDAL
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: