diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/entity/Person.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/entity/Person.java new file mode 100644 index 0000000000..0f58e403ab --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/entity/Person.java @@ -0,0 +1,38 @@ +package com.baeldung.spring.data.jpa.listrepositories.entity; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +@Entity +public class Person { + + @Id + private int id; + private String firstName; + private String lastName; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + +} diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/repository/PersonRepository.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/repository/PersonRepository.java new file mode 100644 index 0000000000..0397cf5063 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/repository/PersonRepository.java @@ -0,0 +1,19 @@ +package com.baeldung.spring.data.jpa.listrepositories.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.spring.data.jpa.listrepositories.entity.Person; + +@Repository +public interface PersonRepository extends JpaRepository { + Person findByFirstName(String firstName); + + List findByLastName(String lastName); + + Person findOneByFirstName(String firstName); + + List findOneByLastName(String lastName); +} diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/resources/application.properties b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/application.properties index 5a1841e2ad..cd6dbe3994 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/resources/application.properties +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/application.properties @@ -1,2 +1,5 @@ +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.jpa.hibernate.ddl-auto=none + logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/resources/data.sql b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/data.sql new file mode 100644 index 0000000000..50bd326155 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/data.sql @@ -0,0 +1,4 @@ +INSERT INTO person (id, first_name, last_name) VALUES(1, 'Azhrioun', 'Abderrahim'); +INSERT INTO person (id, first_name, last_name) VALUES(2, 'Brian', 'Wheeler'); +INSERT INTO person (id, first_name, last_name) VALUES(3, 'Stella', 'Anderson'); +INSERT INTO person (id, first_name, last_name) VALUES(4, 'Stella', 'Wheeler'); \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/resources/schema.sql b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/schema.sql new file mode 100644 index 0000000000..c8be0eb781 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/schema.sql @@ -0,0 +1,6 @@ +DROP TABLE IF EXISTS person; +CREATE TABLE person( + id INT PRIMARY KEY, + first_name VARCHAR(200), + last_name VARCHAR(200) +) \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/repository/PersonRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/repository/PersonRepositoryIntegrationTest.java new file mode 100644 index 0000000000..f93b822ffb --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/repository/PersonRepositoryIntegrationTest.java @@ -0,0 +1,66 @@ +package com.baeldung.spring.data.jpa.listrepositories.repository; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.dao.IncorrectResultSizeDataAccessException; + +import com.baeldung.spring.data.jpa.listrepositories.entity.Person; + +@DataJpaTest +class PersonRepositoryIntegrationTest { + + @Autowired + private PersonRepository personRepository; + + @Test + void givenFirstName_whenCallingFindByFirstName_ThenReturnOnePerson() { + Person person = personRepository.findByFirstName("Azhrioun"); + + assertNotNull(person); + assertEquals("Abderrahim", person.getLastName()); + } + + @Test + void givenLastName_whenCallingFindByLastName_ThenReturnList() { + List person = personRepository.findByLastName("Wheeler"); + + assertEquals(2, person.size()); + } + + @Test + void givenFirstName_whenCallingFindOneByFirstName_ThenReturnOnePerson() { + Person person = personRepository.findOneByFirstName("Azhrioun"); + + assertNotNull(person); + assertEquals("Abderrahim", person.getLastName()); + } + + @Test + void givenLastName_whenCallingFindOneByLastName_ThenReturnList() { + List persons = personRepository.findOneByLastName("Wheeler"); + + assertEquals(2, persons.size()); + } + + @Test + void givenFirstName_whenCallingFindByFirstName_ThenThrowException() { + IncorrectResultSizeDataAccessException exception = assertThrows(IncorrectResultSizeDataAccessException.class, () -> personRepository.findByFirstName("Stella")); + + assertEquals("query did not return a unique result: 2", exception.getMessage()); + } + + @Test + void givenFirstName_whenCallingFindOneByFirstName_ThenThrowException() { + IncorrectResultSizeDataAccessException exception = assertThrows(IncorrectResultSizeDataAccessException.class, () -> personRepository.findOneByFirstName("Stella")); + + assertEquals("query did not return a unique result: 2", exception.getMessage()); + } + +}