From c4ed1770e266a0b9395a05fd47a2b04f82d47215 Mon Sep 17 00:00:00 2001 From: Arindum Roy Date: Sun, 19 Apr 2020 21:46:19 +0530 Subject: [PATCH 1/8] "first commit in new fork" --- .../baeldung/composite/BookApplication.java | 12 ++++ .../com/baeldung/composite/entity/Book.java | 47 ++++++++++++++ .../com/baeldung/composite/entity/BookId.java | 51 +++++++++++++++ .../composite/repository/BookRepository.java | 16 +++++ .../repository/BookRepositoryTest.java | 63 +++++++++++++++++++ 5 files changed, 189 insertions(+) create mode 100644 persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/BookApplication.java create mode 100644 persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/Book.java create mode 100644 persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/BookId.java create mode 100644 persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/repository/BookRepository.java create mode 100644 persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryTest.java diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/BookApplication.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/BookApplication.java new file mode 100644 index 0000000000..52f06058aa --- /dev/null +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/BookApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.composite; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class BookApplication { + + public static void main(String[] args) { + SpringApplication.run(BookApplication.class); + } +} diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/Book.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/Book.java new file mode 100644 index 0000000000..e4f1727654 --- /dev/null +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/Book.java @@ -0,0 +1,47 @@ +package com.baeldung.composite.entity; + +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; + +@Entity +public class Book { + + @EmbeddedId + private BookId id; + private String genre; + private Integer price; + + public Book() { + } + + public Book(String author, String name, String genre, Integer price) { + BookId id = new BookId(author, name); + this.id = id; + this.genre = genre; + this.price = price; + } + + public BookId getId() { + return id; + } + + public void setId(BookId id) { + this.id = id; + } + + public String getGenre() { + return genre; + } + + public void setGenre(String genre) { + this.genre = genre; + } + + public Integer getPrice() { + return price; + } + + public void setPrice(Integer price) { + this.price = price; + } +} diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/BookId.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/BookId.java new file mode 100644 index 0000000000..1524452412 --- /dev/null +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/BookId.java @@ -0,0 +1,51 @@ +package com.baeldung.composite.entity; + +import javax.persistence.Embeddable; +import java.io.Serializable; +import java.util.Objects; + +@Embeddable +public class BookId implements Serializable { + + private String author; + private String name; + + public BookId() { + } + + public BookId(String author, String name) { + this.author = author; + this.name = name; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + BookId bookId = (BookId) o; + return Objects.equals(author, bookId.author) && Objects.equals(name, bookId.name); + } + + @Override + public int hashCode() { + return Objects.hash(author, name); + } +} diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/repository/BookRepository.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/repository/BookRepository.java new file mode 100644 index 0000000000..3c09f909f0 --- /dev/null +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/repository/BookRepository.java @@ -0,0 +1,16 @@ +package com.baeldung.composite.repository; + +import com.baeldung.composite.entity.Book; +import com.baeldung.composite.entity.BookId; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface BookRepository extends JpaRepository { + + List findByIdName(String name); + + List findByIdAuthor(String author); +} diff --git a/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryTest.java b/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryTest.java new file mode 100644 index 0000000000..590593982d --- /dev/null +++ b/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryTest.java @@ -0,0 +1,63 @@ +package com.baeldung.composite.repository; + +import com.baeldung.composite.BookApplication; +import com.baeldung.composite.entity.Book; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = BookApplication.class) +public class BookRepositoryTest { + + public static final String JAVA_101 = "Java101"; + public static final String JANE = "Jane"; + @Autowired + BookRepository repository; + + @Before + public void setUp() { + Book book1 = new Book("John", JAVA_101, "Tech", 20); + Book book2 = new Book(JANE, JAVA_101, "Arch", 25); + Book book3 = new Book(JANE, "Scala101", "Tech", 23); + + repository.saveAll(Arrays.asList(book1, book2, book3)); + } + + @After + public void tearDown() { + repository.deleteAll(); + } + + @Test + public void testFindByName() { + List books = repository.findByIdName(JAVA_101); + + assertEquals(2, books.size()); + } + + @Test + public void testFindByAuthor() { + List books = repository.findByIdAuthor(JANE); + + assertEquals(2, books.size()); + } + + @Test + public void testFindByGenre() { + List books = repository.findByIdAuthor(JANE); + + assertEquals(2, books.size()); + } +} \ No newline at end of file From 60b00e38b6c5e1940bdbb7bae6baf38925765f1b Mon Sep 17 00:00:00 2001 From: Arindum Roy Date: Sun, 19 Apr 2020 23:03:39 +0530 Subject: [PATCH 2/8] "first commit in new fork" --- ...okRepositoryTest.java => BookRepositoryIntegrationTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/{BookRepositoryTest.java => BookRepositoryIntegrationTest.java} (97%) diff --git a/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryTest.java b/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java similarity index 97% rename from persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryTest.java rename to persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java index 590593982d..362950bea9 100644 --- a/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryTest.java +++ b/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java @@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @SpringBootTest(classes = BookApplication.class) -public class BookRepositoryTest { +public class BookRepositoryIntegrationTest { public static final String JAVA_101 = "Java101"; public static final String JANE = "Jane"; From 5bfa01ec05e0d3d9364a042c307425f370877691 Mon Sep 17 00:00:00 2001 From: Arindum Roy Date: Sat, 2 May 2020 00:20:16 +0530 Subject: [PATCH 3/8] "spring-data-jpa-5 new module" --- .../baeldung/composite/BookApplication.java | 12 ---- .../com/baeldung/composite/entity/Book.java | 47 -------------- .../com/baeldung/composite/entity/BookId.java | 51 --------------- .../composite/repository/BookRepository.java | 16 ----- .../BookRepositoryIntegrationTest.java | 63 ------------------- 5 files changed, 189 deletions(-) delete mode 100644 persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/BookApplication.java delete mode 100644 persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/Book.java delete mode 100644 persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/BookId.java delete mode 100644 persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/repository/BookRepository.java delete mode 100644 persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/BookApplication.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/BookApplication.java deleted file mode 100644 index 52f06058aa..0000000000 --- a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/BookApplication.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.composite; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class BookApplication { - - public static void main(String[] args) { - SpringApplication.run(BookApplication.class); - } -} diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/Book.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/Book.java deleted file mode 100644 index e4f1727654..0000000000 --- a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/Book.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.composite.entity; - -import javax.persistence.EmbeddedId; -import javax.persistence.Entity; - -@Entity -public class Book { - - @EmbeddedId - private BookId id; - private String genre; - private Integer price; - - public Book() { - } - - public Book(String author, String name, String genre, Integer price) { - BookId id = new BookId(author, name); - this.id = id; - this.genre = genre; - this.price = price; - } - - public BookId getId() { - return id; - } - - public void setId(BookId id) { - this.id = id; - } - - public String getGenre() { - return genre; - } - - public void setGenre(String genre) { - this.genre = genre; - } - - public Integer getPrice() { - return price; - } - - public void setPrice(Integer price) { - this.price = price; - } -} diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/BookId.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/BookId.java deleted file mode 100644 index 1524452412..0000000000 --- a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/entity/BookId.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.composite.entity; - -import javax.persistence.Embeddable; -import java.io.Serializable; -import java.util.Objects; - -@Embeddable -public class BookId implements Serializable { - - private String author; - private String name; - - public BookId() { - } - - public BookId(String author, String name) { - this.author = author; - this.name = name; - } - - public String getAuthor() { - return author; - } - - public void setAuthor(String author) { - this.author = author; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - BookId bookId = (BookId) o; - return Objects.equals(author, bookId.author) && Objects.equals(name, bookId.name); - } - - @Override - public int hashCode() { - return Objects.hash(author, name); - } -} diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/repository/BookRepository.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/repository/BookRepository.java deleted file mode 100644 index 3c09f909f0..0000000000 --- a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/composite/repository/BookRepository.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.composite.repository; - -import com.baeldung.composite.entity.Book; -import com.baeldung.composite.entity.BookId; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public interface BookRepository extends JpaRepository { - - List findByIdName(String name); - - List findByIdAuthor(String author); -} diff --git a/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java deleted file mode 100644 index 362950bea9..0000000000 --- a/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.baeldung.composite.repository; - -import com.baeldung.composite.BookApplication; -import com.baeldung.composite.entity.Book; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = BookApplication.class) -public class BookRepositoryIntegrationTest { - - public static final String JAVA_101 = "Java101"; - public static final String JANE = "Jane"; - @Autowired - BookRepository repository; - - @Before - public void setUp() { - Book book1 = new Book("John", JAVA_101, "Tech", 20); - Book book2 = new Book(JANE, JAVA_101, "Arch", 25); - Book book3 = new Book(JANE, "Scala101", "Tech", 23); - - repository.saveAll(Arrays.asList(book1, book2, book3)); - } - - @After - public void tearDown() { - repository.deleteAll(); - } - - @Test - public void testFindByName() { - List books = repository.findByIdName(JAVA_101); - - assertEquals(2, books.size()); - } - - @Test - public void testFindByAuthor() { - List books = repository.findByIdAuthor(JANE); - - assertEquals(2, books.size()); - } - - @Test - public void testFindByGenre() { - List books = repository.findByIdAuthor(JANE); - - assertEquals(2, books.size()); - } -} \ No newline at end of file From 8ef7cc6b81e62d42ca8afa533610cec0cf489337 Mon Sep 17 00:00:00 2001 From: Arindum Roy Date: Sat, 2 May 2020 00:23:59 +0530 Subject: [PATCH 4/8] all files --- .../spring-data-jpa-5/README.md | 11 ++++ persistence-modules/spring-data-jpa-5/pom.xml | 47 ++++++++++++++ .../baeldung/composite/BookApplication.java | 12 ++++ .../com/baeldung/composite/entity/Book.java | 47 ++++++++++++++ .../com/baeldung/composite/entity/BookId.java | 51 +++++++++++++++ .../composite/repository/BookRepository.java | 18 ++++++ .../src/main/resources/application.properties | 2 + .../BookRepositoryIntegrationTest.java | 64 +++++++++++++++++++ .../resources/application-test.properties | 2 + 9 files changed, 254 insertions(+) create mode 100644 persistence-modules/spring-data-jpa-5/README.md create mode 100644 persistence-modules/spring-data-jpa-5/pom.xml create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/BookApplication.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/Book.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/BookId.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/repository/BookRepository.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/resources/application.properties create mode 100644 persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java create mode 100644 persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties diff --git a/persistence-modules/spring-data-jpa-5/README.md b/persistence-modules/spring-data-jpa-5/README.md new file mode 100644 index 0000000000..51c39f7bb0 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/README.md @@ -0,0 +1,11 @@ +### Relevant Articles: +- [Derived Query Methods in Spring Data JPA Repositories](TBD) + +### Eclipse Config +After importing the project into Eclipse, you may see the following error: +"No persistence xml file found in project" + +This can be ignored: +- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project" +Or: +- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator diff --git a/persistence-modules/spring-data-jpa-5/pom.xml b/persistence-modules/spring-data-jpa-5/pom.xml new file mode 100644 index 0000000000..c71cec1d08 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + spring-data-jpa-5 + spring-data-jpa-5 + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + + mysql + mysql-connector-java + + + + org.postgresql + postgresql + + + + com.h2database + h2 + + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/BookApplication.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/BookApplication.java new file mode 100644 index 0000000000..52f06058aa --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/BookApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.composite; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class BookApplication { + + public static void main(String[] args) { + SpringApplication.run(BookApplication.class); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/Book.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/Book.java new file mode 100644 index 0000000000..e4f1727654 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/Book.java @@ -0,0 +1,47 @@ +package com.baeldung.composite.entity; + +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; + +@Entity +public class Book { + + @EmbeddedId + private BookId id; + private String genre; + private Integer price; + + public Book() { + } + + public Book(String author, String name, String genre, Integer price) { + BookId id = new BookId(author, name); + this.id = id; + this.genre = genre; + this.price = price; + } + + public BookId getId() { + return id; + } + + public void setId(BookId id) { + this.id = id; + } + + public String getGenre() { + return genre; + } + + public void setGenre(String genre) { + this.genre = genre; + } + + public Integer getPrice() { + return price; + } + + public void setPrice(Integer price) { + this.price = price; + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/BookId.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/BookId.java new file mode 100644 index 0000000000..1524452412 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/BookId.java @@ -0,0 +1,51 @@ +package com.baeldung.composite.entity; + +import javax.persistence.Embeddable; +import java.io.Serializable; +import java.util.Objects; + +@Embeddable +public class BookId implements Serializable { + + private String author; + private String name; + + public BookId() { + } + + public BookId(String author, String name) { + this.author = author; + this.name = name; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + BookId bookId = (BookId) o; + return Objects.equals(author, bookId.author) && Objects.equals(name, bookId.name); + } + + @Override + public int hashCode() { + return Objects.hash(author, name); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/repository/BookRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/repository/BookRepository.java new file mode 100644 index 0000000000..d5993eaf79 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/repository/BookRepository.java @@ -0,0 +1,18 @@ +package com.baeldung.composite.repository; + +import com.baeldung.composite.entity.Book; +import com.baeldung.composite.entity.BookId; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface BookRepository extends JpaRepository { + + List findByIdName(String name); + + List findByIdAuthor(String author); + + List findByGenre(String genre); +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/resources/application.properties b/persistence-modules/spring-data-jpa-5/src/main/resources/application.properties new file mode 100644 index 0000000000..dfc5e56e33 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.jpa.show-sql=true + diff --git a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java new file mode 100644 index 0000000000..9d25acbd96 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java @@ -0,0 +1,64 @@ +package com.baeldung.composite.repository; + +import com.baeldung.composite.BookApplication; +import com.baeldung.composite.entity.Book; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = BookApplication.class) +public class BookRepositoryIntegrationTest { + + public static final String JAVA_101 = "Java101"; + public static final String JANE = "Jane"; + public static final String TECH = "Tech"; + @Autowired + BookRepository repository; + + @Before + public void setUp() { + Book book1 = new Book("John", JAVA_101, TECH, 20); + Book book2 = new Book(JANE, JAVA_101, "Arch", 25); + Book book3 = new Book(JANE, "Scala101", TECH, 23); + + repository.saveAll(Arrays.asList(book1, book2, book3)); + } + + @After + public void tearDown() { + repository.deleteAll(); + } + + @Test + public void testFindByName() { + List books = repository.findByIdName(JAVA_101); + + assertEquals(2, books.size()); + } + + @Test + public void testFindByAuthor() { + List books = repository.findByIdAuthor(JANE); + + assertEquals(2, books.size()); + } + + @Test + public void testFindByGenre() { + List books = repository.findByGenre(TECH); + + assertEquals(2, books.size()); + } +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties b/persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties new file mode 100644 index 0000000000..f9497c8f37 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties @@ -0,0 +1,2 @@ +spring.jpa.hibernate.ddl-auto=update +spring.datasource.url=jdbc:h2:mem:jpa3 \ No newline at end of file From 5724e188a07a73612802981271b5f367fbc858be Mon Sep 17 00:00:00 2001 From: Arindum Roy Date: Sat, 2 May 2020 00:28:53 +0530 Subject: [PATCH 5/8] "updated README.md" --- persistence-modules/spring-data-jpa-5/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/spring-data-jpa-5/README.md b/persistence-modules/spring-data-jpa-5/README.md index 51c39f7bb0..dd4ac22e4a 100644 --- a/persistence-modules/spring-data-jpa-5/README.md +++ b/persistence-modules/spring-data-jpa-5/README.md @@ -1,5 +1,5 @@ ### Relevant Articles: -- [Derived Query Methods in Spring Data JPA Repositories](TBD) +- [Spring JPA @Embedded and @EmbeddedId](TBD) ### Eclipse Config After importing the project into Eclipse, you may see the following error: From 50a6563e30fd87dc040cdb6d72da0cac9f67928f Mon Sep 17 00:00:00 2001 From: Arindum Roy Date: Sun, 3 May 2020 23:19:33 +0530 Subject: [PATCH 6/8] "updated pom.xml" --- persistence-modules/spring-data-jpa-5/pom.xml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/persistence-modules/spring-data-jpa-5/pom.xml b/persistence-modules/spring-data-jpa-5/pom.xml index c71cec1d08..390cfaf0c0 100644 --- a/persistence-modules/spring-data-jpa-5/pom.xml +++ b/persistence-modules/spring-data-jpa-5/pom.xml @@ -28,16 +28,6 @@ spring-boot-starter-data-jdbc - - mysql - mysql-connector-java - - - - org.postgresql - postgresql - - com.h2database h2 From 43816bc2822b0440860aba1a584dca33d5502d41 Mon Sep 17 00:00:00 2001 From: Arindum Roy Date: Sun, 3 May 2020 23:33:39 +0530 Subject: [PATCH 7/8] update --- persistence-modules/spring-data-jpa-5/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/spring-data-jpa-5/pom.xml b/persistence-modules/spring-data-jpa-5/pom.xml index 390cfaf0c0..7f2c724f98 100644 --- a/persistence-modules/spring-data-jpa-5/pom.xml +++ b/persistence-modules/spring-data-jpa-5/pom.xml @@ -34,4 +34,4 @@ - \ No newline at end of file + From 4487ac8654ac989a607998ae382dadb5979ab173 Mon Sep 17 00:00:00 2001 From: Arindum Roy Date: Sun, 3 May 2020 23:36:14 +0530 Subject: [PATCH 8/8] update --- .../src/test/resources/application-test.properties | 1 - 1 file changed, 1 deletion(-) diff --git a/persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties b/persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties index f9497c8f37..207de2e8ec 100644 --- a/persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties +++ b/persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties @@ -1,2 +1 @@ spring.jpa.hibernate.ddl-auto=update -spring.datasource.url=jdbc:h2:mem:jpa3 \ No newline at end of file