-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Limit selection of nested properties #3352
Comments
Currently, this behavior is by design as we use top-level properties for the selection. We could introspect the projection for nested projections to limit the selection. For the time being, using inlined, concatenated properties makes sense. We can revisit this enhancement somewhere in the future. |
Thank you for the feedback, @mp911de! Would you accept a small addition to the docs about it? I'd be happy to contribute. Talking with colleagues, I realized this wasn't clear to many of them when looking at the |
Would love to see this revisited in the future - if the nested entity has relationships itself that are not part of the projection, they will also get queried in separate queries, so this can result in a lot of wasted DB resources. |
@scordio sure, feel free. |
FWIW, I updated docs with #2757 to explain property limitations. |
spring-data-jpa/src/main/antora/modules/ROOT/pages/repositories/projections.adoc Lines 26 to 27 in 7f13a04
Looks perfect to me. Thanks a lot! |
I explored nested element projections. The good news is, that it works for interface projections quite well. There are a few caveats:
That being said, it seems that there might be a way for nested properties decomposition using various roundtrips. I'm just not sure that things will work in all sorts of arrangements. Given the complexity and uncertainty, I need to consult the team how we want to proceed. |
We've explored this area. Using select alias_1689020768.firstname, alias_1689020768.lastname,
new org.springframework.data.jpa.domain.sample.User(alias_2019149967.firstname as firstname, alias_2019149967.lastname as lastname,
new org.springframework.data.jpa.domain.sample.User(alias_15395037.firstname as firstname, alias_15395037.lastname as lastname) as manager)
from org.springframework.data.jpa.domain.sample.User alias_1689020768
left join alias_1689020768.manager alias_2019149967
left join alias_2019149967.manager alias_15395037
where alias_1689020768.firstname like :firstname escape \ assuming we're querying interface NameAndManagerNameOnly {
String getFirstname();
String getLastname();
NameAndManagerNameOnly getManager();
}
@Table(name = "SD_User")
class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO) private Integer id;
private String firstname;
private String lastname;
@ManyToOne(fetch = FetchType.LAZY) private User manager;
// …
} Using that HQL for query creation fails in the first parse phase with duplicate aliases (although aliases are not duplicate within their scope). Removing aliases fails because constructors do not match; CriteriaBuilder Queries tend to use a different mechanism than constructors, so I expected that to work as well. All in all, Hibernate is capable of returning object graphs from such a query. It would be neat if that kind of arrangement (also, using That being said, we decided not to follow up on that topic as we have other features that we want to ship and don't really have the bandwidth (and energy) to convince the Hibernate team that such an endeavor requires. We can reopen that ticket once there is broader support across JPA providers. |
I totally get it. Even if I were to try, I don't feel like I have the expertise to convince them. Still, many thanks for looking into it! |
I'm not sure if I should report this in
spring-data-jpa
orspring-data-commons
. I apologize in advance if this isn't the right place.I experienced the following issue on Spring Data JPA with Hibernate, initially with Spring Boot 2.7.14 and Db2 for z/OS, but also reproduced with the latest Spring Boot (both GA and snapshot) and H2.
In the Interface-based Projections section of both the Spring Data JPA and Spring Data Commons reference guides, there is an example on how to use such projections recursively:
If I use this interface in a repository, like:
The following query is executed:
where
street
andzip_code
are also selected although they are not required.This seems to contradict what is mentioned in the Closed Projections section:
As a workaround, if I remove the nested interface in
PersonSummary
and concatenate the property names:the following query is executed:
Reproducer: PersonRepositoryTest
Is this a bug or is it by design?
In case the example with the nested interface is not meant to be considered as a closed projection, would it make sense to highlight this detail in the documentation?
The text was updated successfully, but these errors were encountered: