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

FluentQuery ignores .project(...) #3716

Open
gusega opened this issue Dec 13, 2024 · 5 comments
Open

FluentQuery ignores .project(...) #3716

gusega opened this issue Dec 13, 2024 · 5 comments
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged

Comments

@gusega
Copy link

gusega commented Dec 13, 2024

Hi all,

I downloaded main version of spring data (commit sha: 12b5e82)

I enabled query logging and run UserRepositoryTests#findByFluentSpecificationWithSimplePropertyPathsDoesntLoadUnrequestedPaths test with maven profiles hibernate-62 and all-dbs:

In the logs I see the resulting query:

select u1_0.id,u1_0.DTYPE,u1_0.active,u1_0.city,u1_0.country,u1_0.streetName,u1_0.streetNo,
u1_0.age,u1_0.binaryData,u1_0.createdAt,u1_0.dateOfBirth,u1_0.emailAddress,u1_0.firstname,
u1_0.lastname,u1_0.manager_id,u1_0.secondary_email_address 
from SD_User u1_0 where cast(u1_0.firstname as varchar(2147483647)) like ?

I expect to see only u1_0.firstname in the select

The test queries data with

List<User> users = repository.findBy(userHasFirstnameLike("v"), q -> q.project("firstname").all());

FluentQuery#project(...) javadoc says: Define which properties or property paths to include in the query.

@gusega gusega changed the title Fluent query ignores .project() FluentQuery ignores .project(...) Dec 13, 2024
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 13, 2024
@mp911de
Copy link
Member

mp911de commented Dec 16, 2024

JPA imposes quite a few limitations. We cannot select only a few properties when returning an entity. While this can work with Hibernate via multiselect and aliased selection items, JPA 3.2 deprecates multiselect and so this approach isn't usable.

We provide projection properties to JPA as fetchgraph so that joined properties can be selected/skipped when properties are declared.

Technically, we could use projection properties as input properties for DTO and interface projections but from a conceptional perspective, this would for DTO introduce a possibility of defining properties without ensuring that a matching constructor is present. For interface projections, we also select the entire entity (open projection) or selected properties (closed projection) so in both case we would introduce mechanisms that allow breaking functionality.

@mp911de mp911de added the for: team-attention An issue we need to discuss as a team to make progress label Dec 16, 2024
@mp911de
Copy link
Member

mp911de commented Dec 16, 2024

Taking a step backward, what do you want to achieve with that setup and what problem are you trying to solve so that we better understand your context. One problem that can emerge from here is a partially hydrated entity that if saved, can wipe database information.

I have to admit that the test name is somewhat generalized, it actually tests if relations aren't loaded and not simple properties.

@mp911de mp911de added status: waiting-for-feedback We need additional information before we can continue and removed for: team-attention An issue we need to discuss as a team to make progress labels Dec 16, 2024
@gusega
Copy link
Author

gusega commented Dec 16, 2024

@mp911de my entity has a property whose value is a large json document. I only want database to return that json property when application queries a single entity.

When application queries multiple entities, I want database to not return that json property. The application then projects that entity (without json property) result onto Entity_Without_Json_Property.

My application calls repository.findBy(q -> q.as(Entity_Without_Json_Property.class)). The query to the database still queries json property. I added .project([all properties except the json property]). The query still contains json property.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 16, 2024
@mp911de
Copy link
Member

mp911de commented Dec 16, 2024

Care to post the Entity_Without_Json_Property structure? I assume that your use-case will be made available through #3654 that solves also #2327

@gusega
Copy link
Author

gusega commented Dec 16, 2024

@mp911de UserRepositoryTests#findByFluentSpecificationWithSortedInterfaceBasedProjection test from spring-data-jpa is very similar to what my program does:

List<UserProjectionInterfaceBased> users = repository.findBy(userHasFirstnameLike("v"),
				q -> q.as(UserProjectionInterfaceBased.class).sortBy(Sort.by("firstname")).all());

I added .project("firstname") to the fluent query. It had no effect on the resulting database query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants