2014-05-19 2 views
0

다음 코드, 내 데이터베이스에서 특정 프리젠 테이션을 받고 모델 속성으로 추가하고 상세 뷰 반환이 : 나는 '내 관점에서게으른로드 내 컨트롤러에서

@RequestMapping(value = "/detail", method = RequestMethod.GET) 
public String detail(Model model, Principal principal, @RequestParam("id") int id) { 
     Presentation p = presentationDao.get(id); 
     model.addAttribute("presentation", p); 
     return "detail"; 
} 

을 m 사용하여 JSTL 게으른 로딩이있는 프리젠 테이션의 속성을 표시하려고 :

$ {pressentation.student.dossiers.proposal.titel} 그러나

학생이 '위한 서류'의 목록이지만, 지연로드를 사용 중입니다 :

나는 내가 발견 봤 때

Severe: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory 

: 나는 열망에 서류의 FetchType를 설정하면

org.apache.jasper.JasperException: javax.el.ELException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: domein.Student.dossiers, no session or session was closed 

나는 심지어 내 프로젝트를 실행할 수 없습니다 :

@OneToMany(mappedBy = "student", fetch = FetchType.LAZY) 
private List<Dossier> dossiers; 

하지만 나는 다음과 같은 예외가 다음 해결책 :

@RequestMapping(value = "/detail", method = RequestMethod.GET) 
    public String detail(Model model, Principal principal, @RequestParam("id") int id) { 
     Presentatie p = presentatieDao.get(id); 
     Hibernate.initialize(p.getStudent().getDefinitiefDossier().getVoorstel().getTitel()); 
     model.addAttribute("presentatie", p); 
     return "detail"; 
} 

그러나 그것은 다시 다음 예외가 발생합니다.

+0

당신이보기 패턴에서 열기 세션 봤어보기 인터셉터 오픈 세션을 시도? – chrylis

+0

모델에서 FetchType을 작성하면 열심히합니다 –

+0

fetchtype을 열심히 만들 수 없습니다. 게시물에서 말한 것과 같은 예외가 있습니다. 보기 패턴 Chrylis에서 열린 세션이란 무엇을 의미합니까? –

답변

1

컨트롤러에 데이터베이스와 관련된 컨텍스트가 없으므로 컨트롤러에 모든 지연로드 데이터를 전달할 수 없습니다. 당신이해야 할 일은 Model 객체 (아마 presentatieDao DAO)에 메소드를 직접 작성하고 컨트롤러에서 가져 오기 전에이 메소드의 모든 요소를 ​​목록에 추가하는 것입니다.

+0

니스! 나는 그것에 대해 생각하지 않았다. 내 PresentatieDao에서 다음 메소드를 사용하고 있습니다. public Presentatie get (int id) { return em.find (Presentatie.class, id); } em은 내 EntityManager입니다. –

+0

PresentatieDao에서 내 메서드에 무엇을 추가해야할지 모르겠습니다. 너는 어떤 생각을 가지고 있니? 나는 단지 EntityManager에서 find 메소드를 사용한다. –

+0

보기 코드가 세션 및 트랜잭션 경계를 벗어나서 데이터를 느리게로드 할 수 없습니다. 모든 요청에 ​​필요할 것이라는 것을 안다면 데이터를 열심히로드해야합니다. 그래서 열망하는로드를 사용하지 못하는 이유를 파악하는 데 집중해야한다고 생각합니다. – jordan

0

당신은 여러 가지 옵션이 있습니다 : 당신이 열망 컬렉션을

  1. 다른 DAO 방법을 만들고 그 크기는 같은 서류를 (로드하기 위해 당신이 뭔가를 할 presentatieDao.getWithDossiers(id)를 호출() 조서 수집).
  2. 컨트롤러 메서드를 @Transactional으로 표시하면 세션이 열린 상태로 유지되고 제대로 가져올 수 있지만 장기적으로는 응용 프로그램의 속도가 느려질 수 있습니다.

또 다른 하나

  1. http://springtips.blogspot.co.uk/2007/07/open-session-in-view.html
+0

컬렉션을 열망하게 만들면 'EntityManagerFactory를 빌드 할 수 없습니다' 다른 솔루션에서 나에게 다음과 같은 오류 : 'org.hibernate.LazyInitializationException : 프록시를 초기화 할 수 없습니다 - 세션 없음' 너무 지겨워 : P –

+0

심지어 두 번째 및 트랜잭션 ????? – gotch4

+0

시도해보십시오 http://springtips.blogspot.co.uk/2007/07/open-session-in-view.html – gotch4