Spring Data JPA Paging and Sorting example

This article shows how to do Spring Data JPA paging and sorting using the PagingAndSortingRepository interface. Technologies used: Spring Boot 3.1.2 Spring Data JPA H2 in-memory database Java 17 Maven 3 JUnit 5 Spring Integration Tests with TestRestTemplate Unit Tests with Mocking (Mockito) Table of contents: 1. Extends PagingAndSortingRepository 2. Extends JpaRepository 3. Pageable and …

Read more

Testing Spring Data JPA with @DataJpaTest

This article shows how to use @DataJpaTest to test the Spring Data JPA application. Technologies used: Spring Boot 3.1.2 Spring Data JPA (Hibernate 6 is the default JPA implementation) H2 in-memory database Maven Java 17 JUnit 5 Table of contents: 1. Project Directory 2. Spring Data JPA – Entity and Repository 3. @DataJpaTest 3.1 Disable …

Read more

Spring Boot – package javax.persistence does not exist

We migrated the project to Spring Boot 3, and the JPA entity class hits package javax.persistence does not exist. java: package javax.persistence does not exist import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; P.S. Tested with Spring Boot 3.1.2 Solution Spring Boot 3 migrated all Java EE APIs javax.* to their equivalent Jakarta EE variant …

Read more

Spring Boot + Spring Data JPA + PostgreSQL example

This article shows how to use Spring Web MVC to create REST endpoints for CRUD database operations using the Spring Data JPA and PostgreSQL. At the end of the tutorial, we will use Docker to start a PostgreSQL container to test the Spring Boot REST endpoints using curl commands. We will use Spring Test and …

Read more

Spring Boot + Spring Data JPA + MySQL example

This article shows how to use Spring Web MVC to create REST endpoints to perform CRUD database operations using the Spring Data JPA and MySQL. At the end of the tutorial, we will use Docker to start a MySQL container to test the Spring Boot REST endpoints using curl commands. We will use Spring Test …

Read more

Spring Boot + Spring Data JPA example

This article shows how to use Spring Data JPA to perform CRUD operation into a H2 in-memory database. Technologies used: Spring Boot 3.1.2 Spring Data JPA (Hibernate 6 is the default JPA implementation) H2 in-memory database Maven Java 17 Table of contents: 1. Project Directory 2. Project Dependencies 3. Spring Data JPA – Entity 4. …

Read more

Spring + Mockito – Unable to mock save method?

Try to mock a repository save() method, but it is always returning null? P.S Tested with Spring Boot 2 + Spring Data JPA @Test public void save_book_OK() throws Exception { Book newBook = new Book(1L, "Mockito Guide", "mkyong"); when(mockRepository.save(newBook)).thenReturn(newBook); mockMvc.perform(post("/books") .content("{json}") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()); } Solution 1. Mockito uses the equals for argument matching, try …

Read more