How to force eager loading with CrudRepository in spring-data?(如何在 spring-data 中强制使用 CrudRepository 进行预加载?)
问题描述
我有一个包含 List 的实体,因此默认加载 lazy:
I have an entity containing a List that is thus lazy loaded by default:
interface MyEntityRepository extends CrudRepository<MyEntity, Long> {
}
@Entity
public class MyEntity {
@Id
private Long id;
@OneToMany(mappedBy = "bar") //lazy by default
private List<Bar> bars;
}
@Entity
public class Bar {
//some more
}
问题:如何在执行 repository.findOne(id) 时强制预先加载?
Question: How can I force eager loading when executing repository.findOne(id)?
推荐答案
我也需要这个,因为我在一个事务内的服务对象中调用 dao,所以我调用了 get 方法,所以没有例外,我是能够获取记录.类似于 java 8 中的东西:
I needed this too and as I'm calling the dao inside a service object that is insise a transaction I call call the get method so no exception and I was able to fetch the records. Something like in java 8:
public ProductEntity findProduct(int id) {
ProductEntity p = productRepository.findOne(id);
p.getPresentations().stream().count();
return p;
}
p.getPresentations().stream().count(); 将强制提取,我知道这不是一个干净的方法,但它可以同时完成工作
p.getPresentations().stream().count(); will force the fetch, I know is not a clean way to do it but it gets the job done in the mean time
这篇关于如何在 spring-data 中强制使用 CrudRepository 进行预加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 spring-data 中强制使用 CrudRepository 进行预加载?
基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
