Another way to write this line down? JPA stuff

时间:2018-02-01 18:31:17

标签: java spring hibernate spring-data-jpa

The line is:

recipeRepository.findAll().iterator().forEachRemaining(recipeSet::add);

recipeRepository is my Spring data layer. It finds all the recipes in the DB and adds them to the set. I'm not really familiar with lambdas and :: operators, so I'm wondering if there's another way to write argument recipeSet::add down?

1 个答案:

答案 0 :(得分:1)

问题是findAll()方法返回一个迭代器。您可以通过在recipeRepository中覆盖问题来解决问题,如下所示

@Query("SELECT r FROM Recipe r")
Set<Recipe> findAll();

这样它会返回一个集合然后你可以做类似的事情:

Set<Recipe> recipeSet = recipeRepository.findAll();

不做任何转换或迭代