如何在Spring数据中强制使用CrudRepository进行急切加载? [英] How to force eager loading with CrudRepository in spring-data?
问题描述
我有一个包含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数据中强制使用CrudRepository进行急切加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!