我有一个Person类:
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
@ManyToMany(fetch = FetchType.LAZY)
private List<Role> roles;
// etc
}
具有惰性的多对多关系。
在我的控制器中,我有
@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
PersonRepository personRepository;
@RequestMapping("/get")
public @ResponseBody Person getPerson() {
Person person = personRepository.findOne(1L);
return person;
}
}
而PersonRepository只是这段代码,根据这个指南编写的。
public interface PersonRepository extends JpaRepository<Person, Long> {
}
然而,在这个控制器中,我实际上需要延迟数据。我该如何触发它的加载?
试图访问它将会失败,并显示以下信息:
无法初始化代理 - 没有Session:无法惰性地初始化角色集合 no.dusken.momus.model.Person.roles,或其他异常(取决于我尝试什么)。
如果需要,可以查看xml-description。
谢谢。