diff --git a/persistence-modules/spring-data-jpa-repo-2/README.md b/persistence-modules/spring-data-jpa-repo-2/README.md index 0a690fe5a5..be5bab1b56 100644 --- a/persistence-modules/spring-data-jpa-repo-2/README.md +++ b/persistence-modules/spring-data-jpa-repo-2/README.md @@ -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) diff --git a/persistence-modules/spring-data-jpa-repo-2/pom.xml b/persistence-modules/spring-data-jpa-repo-2/pom.xml index 12e178dd49..3109cb5192 100644 --- a/persistence-modules/spring-data-jpa-repo-2/pom.xml +++ b/persistence-modules/spring-data-jpa-repo-2/pom.xml @@ -14,7 +14,10 @@ - + + org.springframework.boot + spring-boot-starter-web + javax.persistence javax.persistence-api @@ -31,7 +34,7 @@ com.h2database h2 - + com.google.guava guava diff --git a/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/like/LikeApplication.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/like/LikeApplication.java similarity index 85% rename from persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/like/LikeApplication.java rename to persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/like/LikeApplication.java index 311aea3001..26a5723977 100644 --- a/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/like/LikeApplication.java +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/like/LikeApplication.java @@ -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; diff --git a/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/like/model/Movie.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/like/model/Movie.java similarity index 94% rename from persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/like/model/Movie.java rename to persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/like/model/Movie.java index bba8bd35c4..f500b61a8a 100644 --- a/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/like/model/Movie.java +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/like/model/Movie.java @@ -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; diff --git a/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/like/repository/MovieRepository.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/like/repository/MovieRepository.java similarity index 90% rename from persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/like/repository/MovieRepository.java rename to persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/like/repository/MovieRepository.java index 241bdd3306..57befb5943 100644 --- a/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/like/repository/MovieRepository.java +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/like/repository/MovieRepository.java @@ -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 { List findByTitleContaining(String title); - + List findByTitleLike(String title); List findByTitleContains(String title); @@ -23,17 +22,17 @@ public interface MovieRepository extends CrudRepository { List findByDirectorEndsWith(String director); List findByTitleContainingIgnoreCase(String title); - + List findByRatingNotContaining(String rating); - + List findByDirectorNotLike(String director); - + @Query("SELECT m FROM Movie m WHERE m.title LIKE %:title%") List searchByTitleLike(@Param("title") String title); - + @Query("SELECT m FROM Movie m WHERE m.rating LIKE ?1%") List 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]}") diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/resources/persistence.properties b/persistence-modules/spring-data-jpa-repo-2/src/main/resources/persistence.properties index 339859a6e8..05cb7a13b9 100644 --- a/persistence-modules/spring-data-jpa-repo-2/src/main/resources/persistence.properties +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/resources/persistence.properties @@ -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 diff --git a/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/like/MovieRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/like/MovieRepositoryIntegrationTest.java similarity index 91% rename from persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/like/MovieRepositoryIntegrationTest.java rename to persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/like/MovieRepositoryIntegrationTest.java index cc96b638ab..291926c127 100644 --- a/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/like/MovieRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/like/MovieRepositoryIntegrationTest.java @@ -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 results = movieRepository.findByTitleContaining("in"); assertEquals(3, results.size()); - + results = movieRepository.findByTitleLike("%in%"); assertEquals(3, results.size()); @@ -60,25 +64,25 @@ public class MovieRepositoryIntegrationTest { List results = movieRepository.searchByTitleLike("in"); assertEquals(3, results.size()); } - + @Test public void givenStartOfRating_SearchFindByRatingStartsWith_ThenMoviesShouldReturn() { List results = movieRepository.searchByRatingStartsWith("PG"); assertEquals(6, results.size()); } - + @Test public void givenLastName_WhenSearchByDirectorEndsWith_ThenMoviesShouldReturn() { List results = movieRepository.searchByDirectorEndsWith("Burton"); assertEquals(1, results.size()); } - + @Test public void givenPartialRating_findByRatingNotContaining_ThenMoviesShouldReturn() { List results = movieRepository.findByRatingNotContaining("PG"); assertEquals(1, results.size()); } - + @Test public void givenPartialDirector_WhenFindByDirectorNotLike_ThenMoviesShouldReturn() { List results = movieRepository.findByDirectorNotLike("An%"); diff --git a/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/repository/FooServiceIntegrationTest.java b/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/repository/FooServiceIntegrationTest.java index 141844fa11..21990afb5e 100644 --- a/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/repository/FooServiceIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/repository/FooServiceIntegrationTest.java @@ -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()); diff --git a/persistence-modules/spring-data-jpa-repo/src/test/resources/test-movie-cleanup.sql b/persistence-modules/spring-data-jpa-repo-2/src/test/resources/test-movie-cleanup.sql similarity index 100% rename from persistence-modules/spring-data-jpa-repo/src/test/resources/test-movie-cleanup.sql rename to persistence-modules/spring-data-jpa-repo-2/src/test/resources/test-movie-cleanup.sql diff --git a/persistence-modules/spring-data-jpa-repo/src/test/resources/test-movie-data.sql b/persistence-modules/spring-data-jpa-repo-2/src/test/resources/test-movie-data.sql similarity index 100% rename from persistence-modules/spring-data-jpa-repo/src/test/resources/test-movie-data.sql rename to persistence-modules/spring-data-jpa-repo-2/src/test/resources/test-movie-data.sql diff --git a/persistence-modules/spring-data-jpa-repo/README.md b/persistence-modules/spring-data-jpa-repo/README.md index 1a95340a97..43097a8c1e 100644 --- a/persistence-modules/spring-data-jpa-repo/README.md +++ b/persistence-modules/spring-data-jpa-repo/README.md @@ -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: