2017-10-20 108 views
0

我正在嘗試使用jerseyspring boot jpahibernate通過休息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的OpenEntityManagerInViewInterceptorpreHandle方法被稱爲我撥打電話到存儲庫。不應該之前被調用?

什麼可能導致此異常/行爲。我的設置有什麼問題?

回答

0

這是懶惰的模式導致此異常嘗試編輯您的代碼,並使用這個代替:

@OneToMany(mappedBy="foreignKey", fetch = FetchType.EAGER) 
private Set<SomeOtherEntity> someOtherEntity; 
相關問題