我有一个 Person 类:
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
@ManyToMany(fetch = FetchType.LAZY)
private List<Role> roles;
// etc
}
具有惰性的多对多关系。
在我的 Controller 中
@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
PersonRepository personRepository;
@RequestMapping("/get")
public @ResponseBody Person getPerson() {
Person person = personRepository.findOne(1L);
return person;
}
}
而PersonRepository就是这段代码,根据this guide写的
public interface PersonRepository extends JpaRepository<Person, Long> {
}
但是,在这个 Controller 中 我实际上需要惰性数据。如何触发它的加载?
尝试访问它会失败
failed to lazily initialize a collection of role: no.dusken.momus.model.Person.roles, could not initialize proxy - no Session
或其他异常(exception)情况,具体取决于我的尝试。
我的xml-description ,以备不时之需。
谢谢。
最佳答案
您必须对惰性集合进行显式调用才能对其进行初始化(通常的做法是为此目的调用 .size()
)。在 Hibernate 中有一个专用的方法(Hibernate.initialize()
),但 JPA 没有等效的方法。当然,当 session 仍然可用时,您必须确保调用已完成,因此请使用 @Transactional
注释您的 Controller 方法。另一种方法是在 Controller 和 Repository 之间创建一个中间服务层,它可以公开初始化惰性集合的方法。
更新:
请注意,上述解决方案很简单,但会导致对数据库的两个不同查询(一个针对用户,另一个针对其角色)。如果您想获得更好的性能,请将以下方法添加到您的 Spring Data JPA 存储库接口(interface):
public interface PersonRepository extends JpaRepository<Person, Long> {
@Query("SELECT p FROM Person p JOIN FETCH p.roles WHERE p.id = (:id)")
public Person findByIdAndFetchRolesEagerly(@Param("id") Long id);
}
此方法将使用 JPQL 的 fetch join子句在到数据库的单次往返中急切地加载角色关联,因此将减轻上述解决方案中两个不同查询导致的性能损失。
关于java - 如何在 Spring Controller 中获取与 JPA 和 Hibernate 的 FetchType.LAZY 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15359306/