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

Support projections on repository query methods

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.10 M1 (Hopper)
    • Component/s: None
    • Labels:
      None
    • Sprint:
      Hopper M1

      Description

      It would be cool if we could leverage the projection support recently introduced in Spring Data CMNS
      for custom queries in Spring Data JPA.

      This would enable some interesting use cases:
      1) A Projection interface could serve as a view for a domain object - by simply proxying it.
      This could be either a JDK proxy with the actual entity as a target or a dynamically generated
      class (e.g. via ASM) that is only backed by the properties from the target domain object.
      Another valid option would be to use a dedicated DTO type here with properties matching those of the entity.

      2) We could push down the defined properties on the projection interface down to the JPA query creation
      which would then only fetch the values for the properties listed on said interface. This could then be based on 1).

      For a user it could look like this:

      @Entity
      class User{
      	@Id Long id;
      	String firstname;
      	String lastname;
      	Address homeAddress;
      
      	//..get/set
      }
      
      @Projection(type=User.class) // could also be derived by the Repository entity type
      interface UserInfo{
      
      	Long getId();
      	String getFirstname();
      	String getLastname();
      }
      
      interface UserRepository extends CrudRepository<User,Long>{
         
         //... other methods
      
         // here we return an the projection interface from a derived query method.
         List<UserInfo> findAllUserInfosBy()	
      }
      

      I have a prototype for this working that I'll push in a bit.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              olivergierke Oliver Drotbohm
              Reporter:
              thomasd Thomas Darimont
              Last updater:
              Julian Hochstetter
              Votes:
              3 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: