Skip to content

Repository projections not working when projection type is part of entity object hierarchy #3092

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

Closed
shollander opened this issue May 6, 2024 · 1 comment
Assignees
Labels
type: documentation A documentation update

Comments

@shollander
Copy link

The use of class or interface projection by declaring the return type of the repository method to be a class or interface with a limited number of fields does not work properly when the projection class or interface is part of the object hierarchy of the Entity object.

For example, given:

@Document("collection=my_entities")
public class MyEntity implements ContainsName {

private String id;
private String name;
private String other;

// getters/setters
}
public interface ContainsName {
    String getName();
}
public interface MyEntityRepository extends MongoRepository<MyEntity, String> {
    ContainsName findNameOnlyByName(String name);
}

The return value of MyEntityRepository.findNameOnlyByName("someName") will be a MyEntity object that contains all the fields of entity, not just fields defined in the given interface. The behavior is the same whether ContainsName is an interface or an abstract class. Only when the return type of the method is outside of the entity object hierarchy will a true projection query created. While this issue does not cause errors, it is a performance issue since the benefits of using a projection and returning a limited set of data are not realized.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 6, 2024
@mp911de
Copy link
Member

mp911de commented May 7, 2024

ContainsName is not a projection but a supertype. We do not distinguish between interface supertypes or superclasses. Instead, if a repository query method defines a supertype return type, we materialize the actual object. Only classes and interfaces outside the object hierarchy are considered projections.

Our documentation doesn't call this aspect out explicitly, so it would be good to improve our documentation.

@mp911de mp911de added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels May 7, 2024
@mp911de mp911de transferred this issue from spring-projects/spring-data-mongodb May 7, 2024
@mp911de mp911de self-assigned this May 7, 2024
@mp911de mp911de closed this as completed in 18d31ec May 7, 2024
@mp911de mp911de added this to the 3.1.12 (2023.0.12) milestone May 7, 2024
natedanner pushed a commit to natedanner/spring-projects__spring-data-commons that referenced this issue May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

3 participants