I recently started working with Spring data jpa.
It would be highly appreciable if somebody could throw some light on the technical differences between Spring Data JPA's findFirst
and findTop
.
Differences, usages.
Thanks
A spring alternative to spring-data-rest is using spring MVC directly to create a REST API on your own. Spring-data-jpa would still be used to implement the data access layer. Spring MVC is very powerful and is used by spring-data-rest under the hood. This gives you full control of the REST layer.
CrudRepository mainly provides CRUD operations. PagingAndSortingRepository provide methods to perform pagination and sorting of records. JpaRepository provides JPA related methods such as flushing the persistence context and deleting of records in batch.
JPA uses EntityManager interface to create/read/delete operation and maintains the persistence context. Hibernate uses Session interface to create/read/delete operation and maintains the persistence context. JPA uses JPQL (Java Persistence Query Language) as Object Oriented Query language for database operations.
You cant actually use both of them in the same application.
From Spring Data JPA - Reference Documentation,
The results of query methods can be limited via the keywords first
or top
, which can be used interchangeably. An optional numeric value can be appended to top/first
to specify the maximum result size to be returned. If the number is left out, a result size of 1 is assumed.
User findFirstByOrderByLastnameAsc(); User findTopByOrderByAgeDesc(); Page<User> queryFirst10ByLastname(String lastname, Pageable pageable); Slice<User> findTop3ByLastname(String lastname, Pageable pageable); List<User> findFirst10ByLastname(String lastname, Sort sort); List<User> findTop10ByLastname(String lastname, Pageable pageable);
The limiting expressions also support the Distinct
keyword. Also, for the queries limiting the result set to one instance, wrapping the result into an Optional is supported.
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