小编Ale*_*nov的帖子

流式查询结果过早关闭 - Spring Data JPA和Hibernate

这是一个存储库,其中包含此问题中的代码,其中包含错误:https: //github.com/agsimeonov/stream-bug

我一直在尝试使用以下代码片段使用Spring Data JPA和Hibernate来传输查询结果(data.txt是一个3000行的文件,每行都有一个数字):

try (Stream<Customer> stream = repository.streamAll()) {
  stream.forEach(customer -> {
    try {
      File data = new File(getClass().getClassLoader().getResource("data.txt").getFile());
      try (BufferedReader reader = new BufferedReader(new FileReader(data))) {
        while (reader.readLine() != null) {
          // Do stuff for the current customer
        }
      }
    } catch (IOException e) {}
    System.out.println(customer);
  });
}
Run Code Online (Sandbox Code Playgroud)

这是域对象:

@Entity
@Table(name = "customer")
public class Customer {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String firstName;
  private String lastName;

  public Customer() {}

  public Customer(String …
Run Code Online (Sandbox Code Playgroud)

hibernate spring-data spring-data-jpa java-stream

7
推荐指数
1
解决办法
5212
查看次数