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

Projections with @Query don't work as expected when projecting to class

    Details

    • Type: Bug
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      When projecting to an interface everything seems to work as expected with the fields are being mapped correctly. When I attempt to project into a class (with a compatible constructor) it all seems work fine unless I use an @Query annotation where it fails with this exception:

      org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.HashMap<?, ?>] to type [uk.co.rbrt.PersonSummaryClass]
      

          // works
          PersonSummaryClass findPersonSummaryClassById(long id);
          
          // doesn't work
          @Query("SELECT name AS name, age AS age FROM Person WHERE id = :id")
          PersonSummaryClass findPersonSummaryClassByIdQuery(@Param("id") long id);
      

      Example Project: https://github.com/roberthunt/spring-data-query-projection

        Activity

        Hide
        olivergierke Oliver Gierke added a comment -

        You should be able to work around this by using a constructor expression in the query declaration, i.e. select new ….PersonSummaryClass(name, age) ….

        Show
        olivergierke Oliver Gierke added a comment - You should be able to work around this by using a constructor expression in the query declaration, i.e. select new ….PersonSummaryClass(name, age) … .

          People

          • Assignee:
            olivergierke Oliver Gierke
            Reporter:
            roberthunt87@gmail.com Robert Hunt
            Last updater:
            Oliver Gierke
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: