Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support projections on repository query methods [DATAJPA-804] #1165

Closed
spring-projects-issues opened this issue Sep 23, 2015 · 8 comments
Closed

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Sep 23, 2015

Thomas Darimont opened DATAJPA-804 and commented

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


Issue Links:

  • DATAJPA-864 Query execution fails for manually defined queries containing a constructor expression

  • DATACMNS-89 Support for projections in repository query methods

3 votes, 5 watchers

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 23, 2015

Thomas Darimont commented

The same approach would probably also work for other stores like MongoDB and Couchbase

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 27, 2015

Oliver Drotbohm commented

I've put a bit of stuff into feature branches and the Spring Data examples project. Most of the code is in Spring Data Commons, JPA uses the new API to optimize for closed projection interfaces (i.e. ones not using dynamic properties) as well as forwarding constructor expressions for DTOs

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jan 26, 2016

Chris Li commented

Do we have plan when to release this useful feature? :)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jan 26, 2016

Oliver Drotbohm commented

Gonna be part of upcoming Hopper M1

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Feb 11, 2016

Oliver Drotbohm commented

Implemented as documented in this comment

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Mar 31, 2016

Julian Hochstetter commented

I think this issue is not fixed in the current release although it is also mentioned in the release notes but in my point of view the relevant changes remains only in the issue/DATAJPA-804 branch.
Or do i miss anything?

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Mar 31, 2016

Julian Hochstetter commented

Ah i see, but not in the 1.9.4.RELEASE, its in 1.10.0.M1 and my fault was to rely on the 1.9.4.RELEASE changelog.
Oliver, thank you for clearance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants