diff --git a/persistence-modules/spring-data-jpa-query-3/pom.xml b/persistence-modules/spring-data-jpa-query-3/pom.xml index 135d31aaba..18df57fe14 100644 --- a/persistence-modules/spring-data-jpa-query-3/pom.xml +++ b/persistence-modules/spring-data-jpa-query-3/pom.xml @@ -5,6 +5,9 @@ 4.0.0 spring-data-jpa-query-3 spring-data-jpa-query-3 + + 0.15 + com.baeldung @@ -22,6 +25,11 @@ com.h2database h2 + + com.github.javafaker + javafaker + ${javafaker.version} + org.springframework.boot spring-boot-starter-test diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/collectionsvsstream/ListVsStreamQueryApplication.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/collectionsvsstream/ListVsStreamQueryApplication.java new file mode 100644 index 0000000000..58123afa6c --- /dev/null +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/collectionsvsstream/ListVsStreamQueryApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.data.jpa.collectionsvsstream; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ListVsStreamQueryApplication { + + public static void main(String[] args) { + SpringApplication.run(ListVsStreamQueryApplication.class, args); + } +} diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/collectionsvsstream/User.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/collectionsvsstream/User.java new file mode 100644 index 0000000000..d2174c343f --- /dev/null +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/collectionsvsstream/User.java @@ -0,0 +1,61 @@ +package com.baeldung.spring.data.jpa.collectionsvsstream; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "_user") +public class User { + private String firstName; + private String lastName; + private int age; + @Id + private int id; + + public User() { + } + + public User(String firstName, String lastName, int age) { + this.firstName = firstName; + this.lastName = lastName; + this.age = age; + } + + public User(String firstName, String lastName, int age, int id) { + this(firstName, lastName, age); + this.id = id; + } + + 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; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/collectionsvsstream/UserRepository.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/collectionsvsstream/UserRepository.java new file mode 100644 index 0000000000..ed37cb7036 --- /dev/null +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/collectionsvsstream/UserRepository.java @@ -0,0 +1,14 @@ +package com.baeldung.spring.data.jpa.collectionsvsstream; + +import java.util.List; +import java.util.stream.Stream; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserRepository extends JpaRepository { + Stream findAllByAgeGreaterThan(int age); + + List findByAgeGreaterThan(int age); +} diff --git a/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/collectionsvsstream/UserRepositoryUnitTest.java b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/collectionsvsstream/UserRepositoryUnitTest.java new file mode 100644 index 0000000000..3a0342bf41 --- /dev/null +++ b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/collectionsvsstream/UserRepositoryUnitTest.java @@ -0,0 +1,58 @@ +package com.baeldung.spring.data.jpa.collectionsvsstream; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; + +import com.github.javafaker.Faker; + +@DataJpaTest +class UserRepositoryUnitTest { + + @Autowired + private UserRepository userRepository; + + @BeforeEach + public void setup() { + Faker faker = new Faker(); + List people = IntStream.range(1, 100) + .parallel() + .mapToObj(i -> new User(faker.name() + .firstName(), faker.name() + .lastName(), faker.number() + .numberBetween(1, 100), i)) + .collect(Collectors.toList()); + userRepository.saveAll(people); + } + + @AfterEach + public void tearDown() { + userRepository.deleteAll(); + } + + @Test + public void whenAgeIs20_thenItShouldReturnAllUsersWhoseAgeIsGreaterThan20InAList() { + List users = userRepository.findByAgeGreaterThan(20); + assertThat(users).isNotEmpty(); + assertThat(users.stream() + .map(User::getAge) + .allMatch(age -> age > 20)).isTrue(); + } + + @Test + public void whenAgeIs20_thenItShouldReturnAllUsersWhoseAgeIsGreaterThan20InAStream() { + Stream users = userRepository.findAllByAgeGreaterThan(20); + assertThat(users).isNotNull(); + assertThat(users.map(User::getAge) + .allMatch(age -> age > 20)).isTrue(); + } +}