diff --git a/data/data-jpa/src/main/java/com/example/data/jpa/AuthorRepository.java b/data/data-jpa/src/main/java/com/example/data/jpa/AuthorRepository.java index 794f72b6..0938b488 100644 --- a/data/data-jpa/src/main/java/com/example/data/jpa/AuthorRepository.java +++ b/data/data-jpa/src/main/java/com/example/data/jpa/AuthorRepository.java @@ -15,6 +15,9 @@ public interface AuthorRepository extends ListCrudRepository { @Query("SELECT a FROM Author a WHERE a.name = :name") Optional queryFindByName(String name); + @Query(value = "SELECT * FROM author WHERE name = :name", nativeQuery = true) + Optional nativeQueryFindByName(String name); + Stream findByNameContaining(String name); } diff --git a/data/data-jpa/src/main/java/com/example/data/jpa/TransactionalRunner.java b/data/data-jpa/src/main/java/com/example/data/jpa/TransactionalRunner.java index 85c520f2..dce41b64 100644 --- a/data/data-jpa/src/main/java/com/example/data/jpa/TransactionalRunner.java +++ b/data/data-jpa/src/main/java/com/example/data/jpa/TransactionalRunner.java @@ -52,7 +52,7 @@ private void deleteAll() { private void queryFindByName() { Author author1 = this.authorRepository.queryFindByName("Josh Long").orElse(null); - Author author2 = this.authorRepository.queryFindByName("Martin Kleppmann").orElse(null); + Author author2 = this.authorRepository.nativeQueryFindByName("Martin Kleppmann").orElse(null); System.out.printf("queryFindByName(): author1 = %s%n", author1); System.out.printf("queryFindByName(): author2 = %s%n", author2);