2011-02-04 1 views
0

임은 둘 사이의 다 대다 관계가있다.최대 절전 관계 JSTL 질문

컨트롤러 (발췌) :

List<Person> persons = personManager.getPersons(); 
    Map<String, Object> myModel = new HashMap<String, Object>(); 
    myModel.put("persons", persons); 

    return new ModelAndView("WEB-INF/jsp/hello.jsp","model",myModel); 
를 각 사람은 내가 personinstance.getEvents()을 heres 내가 작업 할 것을

를 사용하여 프로그래밍 방식으로 액세스 할 수는 Person.class의 이벤트 세트가 있습니다

JSP 페이지 :

<%@ include file="include.jsp"%> 
<html> 
<head> 
<title>Hello</title> 
</head> 
<body> 
<h1>Persons</h1> 
<br /> 
<c:forEach items="${model.persons}" var="person"> 
    <c:out value="${person.firstname }" /> 
    <c:out value="${person.lastname }" /> 

    <c:forEach items="${person.events }" var="event"> 
     <c:out value="${event.title }" /> 
    </c:forEach> 

    <br /> 
</c:forEach> 
</html> 

오류 :

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: domain.Person.events, no session or session was closed 

올바른 방법 (jstl을 사용하는 각 루프에 대해 중첩 됨)을 수행 할 것인지 또는 컨트롤러 또는 무언가를 통해 찾고있는 결과를 얻을 수 있는지 확실하지 않습니다. 하지만 몇 가지 조언이 필요

답변

2

여기에 대한 stackoverflow에 몇 가지 비슷한 질문이 있습니다. 친숙한 조언으로 새로운 질문을 열기 전에 예외의 일반적인 부분을 찾으십시오. 이 경우 "세션 또는 세션이 종료되지 않았습니다."

문제는 Hibernate가 Person 목록을로드 할 때로드되지 않은 "이벤트"에보기가 액세스하려고하는 것입니다. 이를 게으른 로딩이라고합니다. 보기에 "이벤트"가 필요하다는 것을 안다면보기에 보내기 전에 미리로드해야합니다. 이를 열심히로드라고합니다. 예를 들어 Person 모델에서 @ManyToMany은 다음과 같이 표시 될 수 있습니다.

@ManyToMany(fetch = FetchType.EAGER)