org.hibernate.LazyInitializationException: could not initialize proxy


org.hibernate.LazyInitializationException: could not initialize proxy

fetch = FecthType.Lazy @ManyToOne(fetch = FetchType.LAZY) 위와 같이 지연 로딩을 할 때 발생하는 문제이다. board와 member와 N 대 1 연관관계를 가진다고 해보자. 먼저 지연 로딩 방식으로 로딩하기에 board 테이블만 가져와서 sout으로 board를 출력하는 건 문제가 없지만 board를 가져온 뒤 getWriter()와 같이 Member entity에 접근하려면 문제가 발생한다. 이는 getWriter()를 하려면 member table을 로딩해야 하는데 이미 데이터베이스와의 연결은 끝난 상태이기 때문이다. 'No Session'이라는 메시지가 이런 경우에 발생한다. 이 문제를 해결하기 위해서는 다시 한번 데이터베이스와의 연결이 필요한데 이때 @Transaction을 사용하면 된다. @Transaction은 해당 메서드를 하나의 트랜잭션으로 처리하라는 의미이다. 트랜잭션으로 처리하면 속성에 따라 다르게 동작하지만, 기본적으...


#fetchtype #lazyloading #spring #springboot #transaction #스프링

원문링크 : org.hibernate.LazyInitializationException: could not initialize proxy