spring boot + jpa + jersey无法初始化代理服务器 - 没有会话
问题描述:
我正在尝试使用jersey
和spring boot jpa
和hibernate
通过休息api查询数据库。spring boot + jpa + jersey无法初始化代理服务器 - 没有会话
我控制器的方法:
public SomeValue doSomething(String param) {
MyEntity entity = myService.queryDB(param);
return conv.convertEntity(entity);
}
我的服务:
@Transactional
public MyEntity queryDB(String param) {
return myRepo.findOne(param);
}
实体:
@Entity
MyEntity {
@Id
@NotNull
private String Id;
@OneToMany(mappedBy="foreignKey", fetch = FetchType.LAZY)
private Set<SomeOtherEntity> someOtherEntity;
}
我甚至明确设置在application.yml属性:
open-in-view: true
我得到以下异常:
failed to lazily initialize a collection of role: entitites.MyEntity.someOtherEntity, could not initialize proxy - no Session
在调试过程中,我可以看到Spring的OpenEntityManagerInViewInterceptor
的preHandle
方法被称为后我拨打电话到存储库。不应该之前被调用?
什么可能导致此异常/行为。我的设置有什么问题?
答
这是懒惰的模式导致此异常尝试编辑您的代码,并使用这个代替:
@OneToMany(mappedBy="foreignKey", fetch = FetchType.EAGER)
private Set<SomeOtherEntity> someOtherEntity;