From 602d8b5a092c03a4c0dc3c4d3965b3c8517d5c6f Mon Sep 17 00:00:00 2001 From: Ekaterina Galkina Date: Mon, 1 Apr 2019 14:03:29 +0500 Subject: [PATCH] BAEL-2477 --- .../java/com/baeldung/customer/Customer.java | 37 +++++++++++ .../baeldung/customer/CustomerRepository.java | 18 +++++ .../CustomerRepositoryIntegrationTest.java | 66 +++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 persistence-modules/spring-data-jpa/src/main/java/com/baeldung/customer/Customer.java create mode 100644 persistence-modules/spring-data-jpa/src/main/java/com/baeldung/customer/CustomerRepository.java create mode 100644 persistence-modules/spring-data-jpa/src/test/java/com/baeldung/customer/CustomerRepositoryIntegrationTest.java diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/customer/Customer.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/customer/Customer.java new file mode 100644 index 0000000000..ed922c3075 --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/customer/Customer.java @@ -0,0 +1,37 @@ +package com.baeldung.customer; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Customer { + + @Id + @GeneratedValue + private long id; + private String name; + private String email; + + public Customer(String name, String email) { + this.name = name; + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + +} diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/customer/CustomerRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/customer/CustomerRepository.java new file mode 100644 index 0000000000..c354ccbb58 --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/customer/CustomerRepository.java @@ -0,0 +1,18 @@ +package com.baeldung.customer; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; + +public interface CustomerRepository extends JpaRepository { + + List findByName(String name); + + List findByNameAndEmail(String name, String email); + + @Query("SELECT c FROM Customer c WHERE (:name is null or c.name = :name) and (:email is null or c.email = :email)") + List findCustomerByNameAndEmail(@Param("name") String name, @Param("email") String email); + +} diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/customer/CustomerRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/customer/CustomerRepositoryIntegrationTest.java new file mode 100644 index 0000000000..c3bd821f52 --- /dev/null +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/customer/CustomerRepositoryIntegrationTest.java @@ -0,0 +1,66 @@ +package com.baeldung.customer; + +import com.baeldung.config.PersistenceConfiguration; +import com.baeldung.config.PersistenceProductConfiguration; +import com.baeldung.config.PersistenceUserConfiguration; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class }) +@RunWith(SpringRunner.class) +public class CustomerRepositoryIntegrationTest { + + @PersistenceContext + private EntityManager entityManager; + + @Autowired + private CustomerRepository repository; + + @Before + public void before() { + entityManager.persist(new Customer("A", "A@example.com")); + entityManager.persist(new Customer("D", null)); + entityManager.persist(new Customer("D", "D@example.com")); + } + + @Test + public void givenQueryMethod_whenEmailIsNull_thenFoundByNullEmail() { + List customers = repository.findByNameAndEmail("D", null); + + assertEquals(1, customers.size()); + Customer actual = customers.get(0); + + assertEquals(null, actual.getEmail()); + assertEquals("D", actual.getName()); + } + + @Test + public void givenQueryMethod_whenEmailIsAbsent_thenIgnoreEmail() { + List customers = repository.findByName("D"); + + assertEquals(2, customers.size()); + } + + @Test + public void givenQueryAnnotation_whenEmailIsNull_thenIgnoreEmail() { + List customers = repository.findCustomerByNameAndEmail("D", null); + + assertEquals(2, customers.size()); + } + + @After + public void cleanUp() { + repository.deleteAll(); + } +} \ No newline at end of file