116

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

List<SomeEntity> findBySomeCondition();

and

List<SomeEntity> findAllBySomeCondition();
Robert Hunt
  • 7,914
  • 5
  • 40
  • 43
Nikita
  • 1,465
  • 3
  • 15
  • 17

5 Answers5

193

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.

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Robert Hunt
  • 7,914
  • 5
  • 40
  • 43
  • Thanks for description – Nikita May 27 '16 at 09:36
  • Thanks for specifying the OrderBy definition , showing that the By is necessary before the OrderBy – Sylvester Mar 06 '19 at 13:40
  • is there anywhere a FULL documentation about query creation (including modifying)? I can find only fragments (e.g. https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation - no word about ..all.., remove..) – Ekaterina Aug 28 '20 at 09:16
  • Then why is it even there? – Evghenii Orenciuc Nov 05 '20 at 19:25
  • 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 Creation](https://docs.spring.io/spring-data/jpa/docs/2.4.1/reference/html/#repositories.query-methods.query-creation) – M. Justin Nov 16 '20 at 22:28
  • Issue [DATACMNS-1833](https://jira.spring.io/browse/DATACMNS-1833) now exists in Spring's issue tracker requesting better documentation of this feature. – M. Justin Nov 18 '20 at 19:17
  • This is now in the [2.3.6 documentation](https://docs.spring.io/spring-data/commons/docs/2.3.6.BUILD-SNAPSHOT/reference/html/#repositories.query-methods.query-creation): "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." – M. Justin Dec 01 '20 at 18:52
  • 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. – Datz Jan 18 '22 at 09:35
4

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.

Zahid Khan
  • 2,130
  • 2
  • 18
  • 31
0

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

Morik
  • 21
  • 3
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

-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

shankarsh15
  • 1,947
  • 1
  • 11
  • 16
  • 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. – Nikita May 16 '16 at 14:37