Spring Data JPA difference between findBy / findAllBy
Asked Answered
D

5

118

Is there any difference when using Spring Data JPA keywords between:

List<SomeEntity> findBySomeCondition();

and

List<SomeEntity> findAllBySomeCondition();
Dillman answered 16/5, 2016 at 12:7 Comment(0)
I
196

No, there is no difference between them, they will execute exactly the same query, the All part is ignored by Spring Data when deriving the query from the method name. The only important bit is the By keyword, anything following it is treated as a field name (with the exception of other keywords like OrderBy which incidentially can lead to some strange looking method names like findAllByOrderByIdAsc).

This means something like this is perfectly valid:

List<SomeEntity> findAnythingYouWantToPutHereBySomeCondition();

And will execute exactly the same SQL query as:

List<SomeEntity> findBySomeCondition();

or

List<SomeEntity> findAllBySomeCondition();

The documentation for the 2.3.6 release of Spring Data discusses this feature:

Any text between find (or other introducing keywords) and By is considered to be descriptive unless using one of the result-limiting keywords such as a Distinct to set a distinct flag on the query to be created or Top/First to limit query results.

The purpose of feature was explained in a blog post about the then-upcoming 2.0 release of Spring Data:

Spring Data’s method parsing uses prefix keywords like find, exists, count, and delete and a terminating By keyword. Everything you put in between find and By makes your method name more expressive and does not affect query derivation.

Inosculate answered 19/5, 2016 at 11:6 Comment(7)
Thanks for specifying the OrderBy definition , showing that the By is necessary before the OrderByKendallkendell
is there anywhere a FULL documentation about query creation (including modifying)? I can find only fragments (e.g. docs.spring.io/spring-data/jpa/docs/current/reference/html/… - no word about ..all.., remove..)Ares
Then why is it even there?Chilblain
I can't find the prefix naming behavior defined in the docs either, but there is one thing that will change the behavior when included in the prefix: the term "Distinct", e.g. "findDistinctBySomeCondition(): 4.4.2. Query CreationEspousal
Issue DATACMNS-1833 now exists in Spring's issue tracker requesting better documentation of this feature.Espousal
This is now in the 2.3.6 documentation: "Any text between find (or other introducing keywords) and By is considered to be descriptive unless using one of the result-limiting keywords such as a Distinct to set a distinct flag on the query to be created or Top/First to limit query results."Espousal
Although there is no technical difference, I think that a method that filters the result with "by" should not be named "findAll", because it just does not find ALL entries.Filigreed
S
5

To illustrate the difference lets look at the two functions:

1. Set<Policy> findAllByRoleIn(Iterable<Role> role);

2. Set<Policy> findByRoleIn(Iterable<Role> role);

The query generated by 1st function:

1.  select policy.id, policy.role from policy where (policy.role in (? , ? , ? , ?))

The query generated by 2nd function:

2. select policy.id, policy.role from policy where (policy.role in (? , ? , ? , ?))

Conclusion: Clearly, if we look at the queries generated by both functions. We can clearly see, there is no difference between the two function definitions, they execute exactly the same query.

Sorkin answered 11/3, 2021 at 7:1 Comment(0)
S
0

one difference is that with findAllBy Hibernate filters (@Filters from org.hibernate.annotations) are applied and so a different sql.

Sackbut answered 22/4, 2020 at 10:31 Comment(0)
F
0

Actually, the difference between findallBy and findby, is that : findAllBy returns a Collection but findBy returns Optional.

so it's preferable to write List findAllBy instead of writing List findBy (but it will work also :p). and to write Optional findBy instead of Optional findAllBy.

check this doc https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.core-concepts

Flattish answered 30/3, 2022 at 19:10 Comment(1)
No, findBy could return a list.Lysimachus
P
-5

findBy method is used if we want to find by name or some other criteria like findByFirstName(String firstName);

findAll methods generally finds by providing specification

List<T> findAll(Specification<T> spec);

Please see docs below for more clarity:

http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html

Proficiency answered 16/5, 2016 at 12:42 Comment(1)
No, that method which you are wrote is from JPASpecificationExecutor. I'm talking about just JpaRepository. I can create method findAllByFirstName(String firstName) and it will work similar to findByFirstName. In this question I'm trying to understand difference between them.Dillman

© 2022 - 2024 — McMap. All rights reserved.