This module presents different ways of how to interact with the database.
- Spring Boot and Java 15
- JPA Criteria API
- QueryDSL
- HQL
- Native Queries
- Jooq
- H2 in memory
Run the main
method in the DemoApplication class.
The JPA Criteria API provides an alternative way for defining JPA queries, which is mainly useful for building dynamic queries whose exact structure is only known at runtime.
Check QueryAnnotationRepository.java
@Query(value = "select * from PERSON where POST_CODE between :zipFrom and :zipTo ORDER BY POST_CODE", nativeQuery = true)
List<Person> findPersonsInZipCodeRangeWithNativeQuery(final int zipFrom, final int zipTo);
@Query(value = "SELECT p FROM PERSON p WHERE p.postCode BETWEEN :zipFrom AND :zipTo")
List<Person> findPersonsInZipCodeRangeWithJPQ(final int zipFrom, final int zipTo, Sort sort);
Pagination allows to just return a subset of a whole result in a Page
. This is useful when navigating through
several pages of data on a web page.
Another advantage of pagination is that the amount of data sent from server to client is minimized.
public interface Pageable {
// number of the current page
int getPageNumber();
int getPageSize();
Sort getSort();
// ... more methods
}
You can choose between various Sort functions and give this as a parameter.