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

Return only specific columns when returning a collection of projections while using the JpaSpecificationExecutor#findBy #3331

Closed
radarsh opened this issue Jan 24, 2024 · 2 comments
Labels
status: duplicate A duplicate of another issue

Comments

@radarsh
Copy link

radarsh commented Jan 24, 2024

I would like to be able to return a collection of projections while using specifications to filter them. I found the newly introduced findBy convenience in the JpaSpecificationExecutor interface but when I try to return a page of projections, it simply ends up retrieving all the columns from the database.

// MyProjection is an interface with nothing special about it

// Here specification is dynamically constructed
return myRepository.findBy(specification, q -> q
            .as(MyProjection.class)
            .page(pageable));

This problem is very similar to the closed #2632. However, I cannot resort to using JPQL or native queries because I have a need to use complex and dynamically constructed specifications to filter out the results.

The table I am querying contains a few JSONB (Postgres) columns that can contain huge JSON documents. So, even though I'm declaring projection interfaces without the JSONB columns, the actual query being made to the database ends up still retrieving the unwanted columns. This causes a massive performance degradation and the approach doesn't scale well.

I'm really out of my depth here and would appreciate any insights or tips.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 24, 2024
@quaff
Copy link
Contributor

quaff commented Jan 25, 2024

It's a very useful feature.

I have a naive idea to implement it but have two problems need to be resolved.

  1. ReturnedType::of is not public
  2. ProjectionFactory need injected by constructor

public <NR> FetchableFluentQuery<NR> as(Class<NR> resultType) {
Assert.notNull(resultType, "Projection target type must not be null");
if (!resultType.isInterface()) {
throw new UnsupportedOperationException("Class-based DTOs are not yet supported.");
}
return new FetchableFluentQueryBySpecification<>(spec, entityType, resultType, sort, limit, properties, finder,
scroll, countOperation, existsOperation, entityManager);
}

		Collection<String> propertiesToUse = this.properties;
		ReturnedType returnedType = ReturnedType.of(resultType, entityType, projectionFactory);
		if (returnedType.isProjecting()) {
			propertiesToUse = returnedType.getInputProperties();
		}
		return new FetchableFluentQueryBySpecification<>(spec, entityType, resultType, sort, limit, propertiesToUse, finder,
				scroll, countOperation, existsOperation, entityManager);

@mp911de
Copy link
Member

mp911de commented Jan 26, 2024

Projection queries do not yet support tuple queries to constrain the selected fields. This is a duplicate of #3144.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Jan 26, 2024
@mp911de mp911de added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants