Skip to content

Spring Framework Blog

Menu
  • Home
  • Spring
  • Spring Boot
  • About
Menu

Spring Boot JPA

Posted on June 7, 2024June 13, 2024 by Armando Marques

Table of Contents

Toggle
  • JPA
  • Repository
  • @DataJpaTest
  • TestEntityManager
  • Spring Data JPA – @Modifying Annotation
  • Queries
  • Pagination and sorting

JPA

@Configuration
@EnableJpaRepositories
@EntityScan

EntityManagerBean – is automatically configured, interacts with persistent context

PlatformTransactionManager – defines basic operation from transactional data access management.

There are 3 main types of repositoriesL

  • JpaRepository
  • CrudRepository
  • PagingAndSortingRepository

Derived query method: subject+predicate

  • findBy
  • countBy
  • deleteBy
  • queryBy
interface PersonRepository extends Repository<Person, Long> {

List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);

// Enables the distinct flag for the query
List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);

// Enabling ignoring case for an individual property
List<Person> findByLastnameIgnoreCase(String lastname);
// Enabling ignoring case for all suitable properties
List<Person> findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);

// Enabling static ORDER BY for a query
List<Person> findByLastnameOrderByFirstnameAsc(String lastname);
List<Person> findByLastnameOrderByFirstnameDesc(String lastname);
}
KeywordSampleJPQL snippet
DistinctfindDistinctByLastnameAndFirstnameselect distinct …​ where x.lastname = ?1 and x.firstname = ?2
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
Is, EqualsfindByFirstname,
findByFirstnameIs
,findByFirstnameEquals
… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age < ?1
LessThanEqualfindByAgeLessThanEqual… where x.age <= ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate < ?1
IsNull, NullfindByAge(Is)Null… where x.age is null
IsNotNull, NotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1 (parameter bound with appended %)
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1 (parameter bound with prepended %)
ContainingfindByFirstnameContaining… where x.firstname like ?1 (parameter bound wrapped in %)
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection<Age> ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection<Age> ages)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstname) = UPPER(?1)

Repository

  • save() – method will detect id is null and create a new instance if needed.
  • saveAll()
  • cascadeall also works with create

All persistent and delete operations require to be executed with a transaction in order for changes to be persisted.

Custom query methods need to be annotated with @Transactional.

@Transactional(readOnly = true)

For read operations, the transaction configuration readOnly flag is set to true

@DataJpaTest

All tests annotated with @DataJpaTest will become transactional but tranasactions are rolleck back at the end of every test.

TestEntityManager

TestEntityManager can be inject in tests

Alternative to EntityManager for use in JPA tests. Provides a subset of EntityManager methods that are useful for tests as well as helper methods for common testing tasks such as persist/flush/find.

Spring Data JPA – @Modifying Annotation

@Modifying annotation in Spring Data JPA allows modification queries such as update and delete and enhances the capabilities of the @Query annotation. It enables data transformation actions beyond simple data retrieval, ensuring transaction integrity and improving performance.

The @Modifying annotation is used to enhance the @Query annotation so that we can not only execute the SELECT queries, but also INSERT, UPDATE, DELETE, and even DDL queries that modify the structure of the underlying database schema using Spring Data JPA

Benefits of @Modifying Annotation

@Modifying annotation is used when you want to execute modifying queries, such as updates or deletes. This annotation is necessary because, by default, Spring Data JPA repository methods are considered read-only and are optimized for querying data, not modifying it. 

  • Improved Performance: Improves performance by allowing changing queries to be executed.
  • Transactional Integrity: Ensures transaction integrity while performing data changes.
  • Support for native queries: Enables native queries for data transformation.
  • Automatic Flush and Clear: Supports automatic flushing and clearing of durability references.

Queries

@Query("select p from Project p when p.code like %exp%")
List<Project> findByCode(@Param("exp") String code)

native query
@Query(native=true, value="select * from project limit 1")
Optional<Project> findSingleProject

Query("select p from Project p where p.name=?1 and p.description=?2")
List<Project> findXXX(String name, String desc)

@Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname")
User findByLastnameOrFirstname(@Param("lastname") String lastname,
@Param("firstname") String firstname);

By default annotated queries return the number of affected entities from the operation.

Spring data does not support sort params in native queries.

Named queries – are predefined static queries with an unique name and query string.

@NamedQuery(name="myNamedQuery", query="...")

<<<<
@Entity
public class Project {
//same method in repo
//
OR
entityManager.createNamedQuery("myNamedQuery",...)...

Pagination and sorting

public interface PagingAndSortingRepository<>
extends Repository<> {
Iterable<> findAll(Sort);
Page<T> findAll(Pageable)
}
///
Pageable x = PageRequest.of(pageNumber,pageSize,sort)
Sort s = Sort.by(Sort.Direction.ASC,"field")

Example of methods:
// findAllByOrderByDueDateDesc()
// findFirst5ByOrderByStatusDesc
  • Spring
  • Documentation
  • References
  • Toc
  • Books
  • Certification
  • AOP
  • Config
  • Java
  • Java core
  • JDBC
  • JPA
  • Rest
  • Security
  • Spring
  • Spring Boot
  • Spring Core
  • Spring Data
  • Spring MVC
  • Spring Rest
  • Spring Security
  • Tests
  • Transactions
  • Uncategorized

Recent Posts

  • Spring Annotations
  • Java Tests
  • Java operators
  • Java versions
  • Java Oracle Licenses
  • Configuration properties
  • MockMvc
  • Spring Security III
  • MVC Controller Method Params
  • JPA Methods
  • Transaction propagation and isolation
  • Spring JDBC
  • Spring Boot Auto-Configuration
  • Spring Resource interface
  • JSR 330 Standard Annotations
  • Spring Aware Interfaces
  • Spring Transactions
  • Spring Boot Core
  • MVC Rest
  • Spring Boot JPA
©2025 Spring Framework Blog | Built using WordPress and Responsive Blogily theme by Superb