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

Implement JpaSpecificationExecutor findFirst() [DATAJPA-810] #1170

Closed
spring-projects-issues opened this issue Oct 9, 2015 · 5 comments
Closed

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Oct 9, 2015

member sound opened DATAJPA-810 and commented

For working with JpaSpecificationExecutor and Specifications, it's crucial that the interface supports the sql LIMIT 1 query, that is achieved in CrudRepository using findFirstBy...

Please implement this in a future release


No further details from DATAJPA-810

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 9, 2015

Oliver Drotbohm commented

I'm not sure I get the issue. findOne(…) is in place, findAll(…) can be used with a Pageable restricted to one. CrudRepository doesn't have a findFirst… method. The interface has been useful to quite a few people so far (at least that's what I've heard)

Loading

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 12, 2015

member sound commented

Ok, let me point that out:

.findFirstBy() on a CrudRepository will create an SQL query with LIMIT 1. @see http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.limit-query-result

.findOne() is designed to find just one existing entity per query. But this requires that there is exactly only one row for the query results.

Imagine there is a _products table, which is queried by attributes that return multiple rows. One would now be interested in sorting the sql furthermore by _price attribute, and then just return the first (=cheapest) result. That's what typically a findFirst(.., sort) function is used in CrudRepository.

But unfortunately the Specifications interface lacks such a method.
Of course, as you wrote, one could use a PageRequest(0, 1, sort) for getting just one single result together with a sort, but that always executes a count( * ) before the select, which is suboptimal in most cases on large databases. It would be much more useful if one could create a LIMIT 1 query with the common syntax findFirstBy(specs, sort).

See also other users having this problem, and how they started to workaround it: https://gist.github.com/tcollins/0ebd1dfa78028ecdef0b

Was that more clear?
Thanks!

Loading

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 9, 2015

member sound commented

Any updates on this? Would you consider this feature for a future release?

Loading

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 15, 2015

Oliver Drotbohm commented

No. If we add findFirst someone will ask for findFirstThree and findLast and findFirst10 etc

Loading

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented May 22, 2019

Jens Schauder commented

Batch closing resolved issue without a fix version and a resolution indicating that there is nothing to release (Won't fix, Invalid ...)

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants