Merge pull request #11615 from hkhan/JAVA-8367-split-spring-data-jpa-repo-module

[JAVA-8367] Split spring-data-jpa-repo module
This commit is contained in:
kwoyke 2021-12-24 10:13:46 +01:00 committed by GitHub
commit 6a239e5166
11 changed files with 37 additions and 26 deletions

View File

@ -3,4 +3,5 @@
### Relevant Articles:
- [Introduction to Spring Data JPA](https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa)
- [Performance Difference Between save() and saveAll() in Spring Data](https://www.baeldung.com/spring-data-save-saveall)
- More articles: [[<-- prev]](/spring-data-jpa-repo/)
- [LIKE Queries in Spring JPA Repositories](https://www.baeldung.com/spring-jpa-like-queries)
- More articles: [[<-- prev]](../spring-data-jpa-repo)

View File

@ -14,7 +14,10 @@
</parent>
<dependencies>
<!-- Persistence -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
@ -31,7 +34,7 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- Utilities -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>

View File

@ -1,4 +1,4 @@
package com.baeldung.like;
package com.baeldung.spring.data.persistence.like;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

@ -1,4 +1,4 @@
package com.baeldung.like.model;
package com.baeldung.spring.data.persistence.like.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;

View File

@ -1,17 +1,16 @@
package com.baeldung.like.repository;
import java.util.List;
package com.baeldung.spring.data.persistence.like.repository;
import com.baeldung.spring.data.persistence.like.model.Movie;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import com.baeldung.like.model.Movie;
import java.util.List;
public interface MovieRepository extends CrudRepository<Movie, Long> {
List<Movie> findByTitleContaining(String title);
List<Movie> findByTitleLike(String title);
List<Movie> findByTitleContains(String title);
@ -23,17 +22,17 @@ public interface MovieRepository extends CrudRepository<Movie, Long> {
List<Movie> findByDirectorEndsWith(String director);
List<Movie> findByTitleContainingIgnoreCase(String title);
List<Movie> findByRatingNotContaining(String rating);
List<Movie> findByDirectorNotLike(String director);
@Query("SELECT m FROM Movie m WHERE m.title LIKE %:title%")
List<Movie> searchByTitleLike(@Param("title") String title);
@Query("SELECT m FROM Movie m WHERE m.rating LIKE ?1%")
List<Movie> searchByRatingStartsWith(String rating);
//Escaping works in SpringBoot >= 2.4.1
//@Query("SELECT m FROM Movie m WHERE m.director LIKE %?#{escape([0])} escape ?#{escapeCharacter()}")
@Query("SELECT m FROM Movie m WHERE m.director LIKE %:#{[0]}")

View File

@ -2,7 +2,7 @@
jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
jdbc.user=sa
jdbc.pass=
jdbc.pass=sa
# hibernate.X
hibernate.hbm2ddl.auto=create-drop

View File

@ -1,7 +1,7 @@
package com.baeldung.like;
package com.baeldung.spring.data.persistence.like;
import com.baeldung.like.model.Movie;
import com.baeldung.like.repository.MovieRepository;
import com.baeldung.spring.data.persistence.like.model.Movie;
import com.baeldung.spring.data.persistence.like.repository.MovieRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -9,6 +9,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import java.util.List;
import static org.junit.Assert.assertEquals;
@ -22,11 +23,14 @@ public class MovieRepositoryIntegrationTest {
@Autowired
private MovieRepository movieRepository;
@Autowired
private DataSource dataSource;
@Test
public void givenPartialTitle_WhenFindByTitleContaining_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.findByTitleContaining("in");
assertEquals(3, results.size());
results = movieRepository.findByTitleLike("%in%");
assertEquals(3, results.size());
@ -60,25 +64,25 @@ public class MovieRepositoryIntegrationTest {
List<Movie> results = movieRepository.searchByTitleLike("in");
assertEquals(3, results.size());
}
@Test
public void givenStartOfRating_SearchFindByRatingStartsWith_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.searchByRatingStartsWith("PG");
assertEquals(6, results.size());
}
@Test
public void givenLastName_WhenSearchByDirectorEndsWith_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.searchByDirectorEndsWith("Burton");
assertEquals(1, results.size());
}
@Test
public void givenPartialRating_findByRatingNotContaining_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.findByRatingNotContaining("PG");
assertEquals(1, results.size());
}
@Test
public void givenPartialDirector_WhenFindByDirectorNotLike_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.findByDirectorNotLike("An%");

View File

@ -7,6 +7,8 @@ import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = PersistenceConfig.class)
public class FooServiceIntegrationTest {
@ -14,6 +16,9 @@ public class FooServiceIntegrationTest {
@Autowired
private IFooService service;
@Autowired
private DataSource dataSource;
@Test(expected = DataIntegrityViolationException.class)
public final void whenInvalidEntityIsCreated_thenDataException() {
service.create(new Foo());

View File

@ -5,13 +5,12 @@ This module contains articles about repositories in Spring Data JPA
### Relevant Articles:
- [Case Insensitive Queries with Spring Data Repository](https://www.baeldung.com/spring-data-case-insensitive-queries)
- [Derived Query Methods in Spring Data JPA Repositories](https://www.baeldung.com/spring-data-derived-queries)
- [LIKE Queries in Spring JPA Repositories](https://www.baeldung.com/spring-jpa-like-queries)
- [Spring Data CrudRepository save() Method](https://www.baeldung.com/spring-data-crud-repository-save)
- [Spring Data JPA Adding a Method in All Repositories](https://www.baeldung.com/spring-data-jpa-method-in-all-repositories)
- [Spring Data Composable Repositories](https://www.baeldung.com/spring-data-composable-repositories)
- [Spring Data JPA Repository Populators](https://www.baeldung.com/spring-data-jpa-repository-populators)
- [Calling Stored Procedures from Spring Data JPA Repositories](https://www.baeldung.com/spring-data-jpa-stored-procedures)
- More articles: [[--> next]](/spring-data-jpa-repo-2/)
- More articles: [[--> next]](../spring-data-jpa-repo-2)
### Eclipse Config
After importing the project into Eclipse, you may see the following error: