Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Technical differences between Spring Data JPA's findFirst and findTop

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

like image 698
Abdullah Khan Avatar asked Jun 27 '16 03:06

Abdullah Khan


People also ask

What is the difference between Spring data JPA and Spring data rest?

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.

What is difference between CrudRepository and JpaRepository?

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.

What is the difference between Spring data JPA and Hibernate?

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.

Can we use Spring data JPA and Hibernate together?

You cant actually use both of them in the same application.


1 Answers

From Spring Data JPA - Reference Documentation,

Limiting query results

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.

Limiting the result size of a query with Top and First

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.

like image 180
SkyWalker Avatar answered Sep 28 '22 01:09

SkyWalker