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

Use of Projection with jpa specification executor #3657

Closed
niyascode007 opened this issue Nov 1, 2024 · 2 comments
Closed

Use of Projection with jpa specification executor #3657

niyascode007 opened this issue Nov 1, 2024 · 2 comments
Labels
status: duplicate A duplicate of another issue

Comments

@niyascode007
Copy link

niyascode007 commented Nov 1, 2024

While creating the specification and trying to use the findBy method the hibernate query shows the result being fetched as the whole entity but the result is in mapped version of projection interface.

public Page<MessageProjection> testJpaSpecProjection(Pageable pageable, LocalDate fromDate, LocalDate toDate,
			String lrn, String mrn, String empfanger)
	{
		Specification<Message> spec = Specification.where(null);
		if (fromDate != null && toDate != null)
		{
			spec = spec.and((root, query, criteriaBuilder) -> criteriaBuilder.between(root.get("messageSentDate"),
					fromDate.atStartOfDay().atOffset(ZoneOffset.UTC), toDate.atStartOfDay().atOffset(ZoneOffset.UTC)));
		}
		if (mrn != null && !mrn.isBlank())
		{
			spec = spec.and((root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("MRN"), mrn));
		}
		if (lrn != null && !lrn.isBlank())
		{
			spec = spec.and((root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("LRN"), lrn));
		}
		if (empfanger != null && !empfanger.isBlank())
		{
			spec = spec.and((root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("recipient"), empfanger));
		}
		return messageRepository.findBy(spec, q -> q.as(MessageProjection.class).page(pageable));
	}
	
	public interface MessageProjection
{
	String getMessageNumber();

	String getHandler();

	String getMRN();

}

@Repository
public interface MessageRepository extends JpaRepository<Message, String>, JpaSpecificationExecutor<Message>
{}
@niyascode007
Copy link
Author

using the projection it should only select those values from the DB it seems like the whole columns are being fetched and mapped to the projection .

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 1, 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 Nov 4, 2024
@mp911de
Copy link
Member

mp911de commented Nov 4, 2024

This is a duplicate and will be fixed via #2327/#3654

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Nov 4, 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

3 participants