private Set<Employee> assignees = new HashSet<>();
public Set<Employee> getAssignees() {
return assignees;
}
public void setAssignees(Set<Employee> assignees) {
this.assignees = assignees;
}
我在下面的代码中使用了上面的方法,当运行该代码时,我得到了这个异常。
`Resolved exception caused by Handler execution: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:`
它来自执行
agendaInfo.setAssignees(item.getAssignees());
行。
为什么会得到这个例外?
@Override
public List<AgendaContentDTO> getAgendasByMeetingId(Long meetingId) {
List<Agenda_content> agendas = extendedAgendaContentRepository.getAgendasByMeetingId(meetingId);
List<AgendaContentDTO> agendaDTOS = new ArrayList<>();
Long currentUser = utils.getCurrentEmployeeId();
for (Agenda_content item: agendas){
AgendaContentDTO agendaInfo = new AgendaContentDTO();
agendaInfo.setId(item.getId());
agendaInfo.setTitle(item.getTitle());
agendaInfo.setContent(item.getContent());
agendaInfo.setMeetingId(item.getMeeting().getId());
agendaInfo.setMeetingMeetingName(item.getMeeting().getMeetingName());
agendaInfo.setAssignees(item.getAssignees());
agendaDTOS.add(agendaInfo);
}
return agendaDTOS;
}
public interface ExtendedAgendaContentRepository extends Agenda_contentRepository {
@Query("select distinct e.meeting from Agenda_content e join e.assignees a where a.id =:assigneeId")
public List<Meeting> getMeetingsAssignedToMe(@Param("assigneeId") long assigneeId);
@Query("select distinct e from Agenda_content e where e.meeting.id =:meetingId")
public List<Agenda_content> getAgendasByMeetingId(@Param("meetingId") long meetingId);
}
答案 0 :(得分:0)
集合在hibernate中是延迟加载的,所以当你从getAgendasByMeetingId加载你的数据时,hibernate加载了Agenda_content列表,但没有加载集合(Assignees)。
LazyInitializationException是geeting,因为没有事务处理(No Session open)可用
SELECT TOP 1 Waiter, sum(tips)Tips
FROM T
GROUP BY waiter
ORDER BY sum(tips) DESC
代码
如果您使用的是spring,请使用@Transaction标记您的方法 或者在休眠状态下,你需要打开一个会话
或者您可以在集合中使用fetchType = FetchType.EAGER,因此hibernate会在加载Agenda_content期间加载所有getAssignees。