这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions persistence-modules/spring-data-jpa-query-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-jpa-query-3</artifactId>
<name>spring-data-jpa-query-3</name>
<properties>
<javafaker.version>0.15</javafaker.version>
</properties>

<parent>
<groupId>com.baeldung</groupId>
Expand All @@ -22,6 +25,11 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>${javafaker.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<User, String> {
Stream<User> findAllByAgeGreaterThan(int age);

List<User> findByAgeGreaterThan(int age);
}
Original file line number Diff line number Diff line change
@@ -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<User> 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<User> users = userRepository.findByAgeGreaterThan(20);
assertThat(users).isNotEmpty();
assertThat(users.stream()
.map(User::getAge)
.allMatch(age -> age > 20)).isTrue();
}

@Test
public void whenAgeIs20_thenItShouldReturnAllUsersWhoseAgeIsGreaterThan20InAStream() {
Stream<User> users = userRepository.findAllByAgeGreaterThan(20);
assertThat(users).isNotNull();
assertThat(users.map(User::getAge)
.allMatch(age -> age > 20)).isTrue();
}
}