如何在 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
相关文章