以下代碼觸發Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.model.entity.WorkflowProcessEntity.workstations, no session or session was closed
錯誤。所以我把這個方法用@Transactional
包裝在一個服務類中,它仍然會拋出錯誤。未能在交易中延遲初始化集合
WorkstationService workstationService = (WorkstationService) ApplicationContextProvider.getApplicationContext().getBean("workstationService");
for (WorkstationEntity workstationEntity : workstationService.getWorkstations(getEntity())) {
registerWorkstation(new ImpositionWorkstation(workstationEntity));
}
WorkstationService.java
@Transactional(readOnly = true)
public Collection<WorkstationEntity> getWorkstations(WorkflowProcessEntity workflowProcessEntity) {
return workflowProcessEntity.getWorkstations();
}
WorkflowProcessEntity.java
@OneToMany(mappedBy = "workflowProcess")
@JsonIgnore
public Collection<WorkstationEntity> getWorkstations() {
return workstations;
}
我怎樣才能正確地查詢這個關係?
發佈異常的完整堆棧跟蹤以及相關代碼。 –
相關代碼如上...它試圖加載getWorkstations()關係。 – Webnet
堆棧跟蹤是? –