All i know so far is that FindBy can return multiple results while FindOneBy will return a single result or null when we use it the following way.
List<Department> findByDepartmentName(String name); Department findOneByDepartmentId(Long Id);
now, my question is, can i use findBy this way?
Department findByDepartmentId(Long Id);
If yes,
Finally, When or Why should i not use findBy in place of findOneBy?
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.
PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.
- findById: This is used if we are not sure that whether the requested entity in the database is present or not. So even if the entity is not present in the database it returns null and doesn't throw any exception. Show activity on this post. getById -> returns a reference proxy to for the actual Entity.
Crud Repository is the base interface and it acts as a marker interface. JPA also provides some extra methods related to JPA such as delete records in batch and flushing data directly to a database. It provides only CRUD functions like findOne, saves, etc. JPA repository also extends the PagingAndSorting repository.
Can I use
findBy
this way? DepartmentfindByDepartmentId(Long Id)
;
Yes, this syntax is technically correct from Spring JPA point of view. Although Spring JPA infers what you're trying to achieve with your query looking at the return type as well.
Basically these are the cases for return types:
with your query you want to return a single value - you can specify basic type
, Entity T
, Optional<T>
, CompletableFuture<T>
etc.
with your query you want to return a collection of T - you can specify List<T>
, Stream<T>
, Page<T>
, Slice<T>
etc.
That being said, your query definition:
Department findByDepartmentId(Long Id);
means that you expect a single result (because you've specified Entity T
as a return type). This will reflect on how Spring JPA executes the query - it will call getSingleResult()
on the javax.persistence.Query
interface, which will throw an exception
if more than one objects satisfy the criteria.
On what basis does
findBydepartmentId
return a single record?
On the basis that there's a single object with that Id, otherwise it will throw an exception.
When or Why should i not use
findBy
in place offindOneBy
?
Those two have different meanings and are not interchangeable.
findOneBy
always results in getSingleResult()
being invoked.
findBy
has different behavior depending on the return type - as per the definitions given above.
findOneByXX
will ensure that there is only one or no value, if there are 2 values an exception will be thrown.
However findByXX
doesn't make this check of uniqueness.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With