记录:jpa返回Iterable而不是list

你可能会好奇为什么仓库返回的是一个 Iterable<T> 而不是 List<T> ,以及为什么需要花费而外的步骤第二步 Iterable<T> 并将它转换为 List<T>.

O/RM 通常会备份 JDBC 结果集中直接返回的实体列表.这样做将改善性能,并允许调用代码在数据库仍在向应用程序返回数据时立即开始执行.不过,从 @Treasanctional 方法中返回将会提交事务并关闭 JDBC 连接,从而关闭结果集.当 O/RM 返回一个List或者其他 Iterable 时,最好迭代列表中所有需要的部分,并在退出事务上下文之前将 Iterable 的内容复制到另一个集合中.这样保证所有实体数据都能在事务关闭之前从数据库中正确读出.

其实,转换放到现在来说并不是一件很难的事:

Iterable<Entity> geted = entityDao.findAll();
List<Entity> list = Lists.newArrays();
geted.forEach(single ->{list.add(single)});

或者

 @RequestMapping("/findAll")
    public Iterable<Person> findAll(){
        List<Person> people = Lists.newArrayList(demoService.findAll());
        return demoService.findAll();
    }

导入

import com.google.common.collect.Lists;

转载链接:https://blog.csdn.net/u010003051/article/details/53422741

猜你喜欢

转载自blog.csdn.net/m18870420619/article/details/82182393