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 进行预加载?
				
        
 
            
        - C++ 和 Java 进程之间的共享内存 2022-01-01
 - value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
 - Java包名称中单词分隔符的约定是什么? 2022-01-01
 - Jersey REST 客户端:发布多部分数据 2022-01-01
 - 将log4j 1.2配置转换为log4j 2配置 2022-01-01
 - 如何使用WebFilter实现授权头检查 2022-01-01
 - Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
 - Eclipse 插件更新错误日志在哪里? 2022-01-01
 - Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
 - 从 finally 块返回时 Java 的奇怪行为 2022-01-01
 
						
						
						
						
						