我有一个包含 List
的实体,因此默认加载 lazy
:
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)
时强制预加载?
最佳答案
您可以使用 left join fetch
强制预先获取编写自定义 HQL 查询,例如:
interface MyEntityRepository extends CrudRepository<MyEntity, Long> {
@Query("select e from MyEntity e left join fetch e.bar b where e.id = ?1")
MyEntity findOne(long id)
}
关于java - 如何在 spring-data 中使用 CrudRepository 强制预加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31854776/