问题描述
我有一个包含 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 进行预加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)