6

I want to use spring projection in my project. I am doing exactly as mentioned at this url http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections

interface NoAddresses {  
  String getFirstName(); 
  String getLastName();  
}

The only difference is my interface is public. and is not in the same package as the repository. Is that a requirement?

Still I see the whole entities are being returned, instead of just the projected columns.

I have

<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.10.2.RELEASE</version>
    </dependency>

Doesn't work. Do i need to add some dependency to make the projection work? I am not using spring-boot but just the spring-data stuff in my project, running on tomcat server.

thanks Chahat

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
user2745862
  • 103
  • 3
  • 8
  • Check out this comprehensive explanation http://stackoverflow.com/questions/29376090/how-to-apply-spring-data-projections-in-a-spring-mvc-controllers from @oliver-gierke. It should cover your problem – Alex Ciocan Dec 09 '16 at 21:04
  • Thanks Alex.. I followed the link. The sql log statements however show that all the columns of the table are queried **then** the complete entities are formed **then** projection is applied to generate projected entity. I am using Projection for performance reasons. but I guess I am infact making it even less performant like this. – user2745862 Dec 10 '16 at 16:07
  • This is the normal workflow. The query is applied to your original domain object and then the result transformed in your projection definition. – Alex Ciocan Dec 10 '16 at 16:14
  • If you want something more performant on this matter, you could check out the hibernate filter mechanism – Alex Ciocan Dec 10 '16 at 20:39
  • Hi Alex, I just came across this post http://stackoverflow.com/questions/40194614/spring-data-jpa-projection-selected-fields-from-the-db the post shows the sql queries being executed under the hood. Spring Projection should work out of the box, the sql should only select the columns in the projection. – user2745862 Dec 27 '16 at 22:04
  • Can you post details of your interface class which extends the JPARepository, you have to be very careful that the query only returns fields which map exactly to your Projection interface getters. – emeraldjava Mar 08 '17 at 16:14

0 Answers0