회원탈퇴 기능을 구현 후 포스트맨을 실행했더니 , 500에러와 함께
콘솔창에 아래와 같은 에러가 발생했다.
2023-01-22 18:11:54.694 ERROR 24825 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.myblog1.user.entity.User.commentList, could not initialize proxy - no Session] with root cause
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.myblog1.user.entity.User.commentList, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:614) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:218) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:591) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:387) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at java.base/java.util.Spliterators$IteratorSpliterator.estimateSize(Spliterators.java:1865) ~[na:na]
at java.base/java.util.Spliterator.getExactSizeIfKnown(Spliterator.java:414) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:539) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:526) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:512) ~[na:na]
at java.base/java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:230) ~[na:na]
at java.base/java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:196) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:239) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.anyMatch(ReferencePipeline.java:632) ~[na:na]
at com.example.myblog1.user.entity.User.hasComment(User.java:61) ~[main/:na]
at com.example.myblog1.comment.service.CommentServiceImpl.validateComment(CommentServiceImpl.java:64) ~[main/:na]
at com.example.myblog1.comment.service.CommentServiceImpl.deleteComment(CommentServiceImpl.java:58) ~[main/:na]
at com.example.myblog1.comment.service.CommentServiceImpl$$FastClassBySpringCGLIB$$2cc68b3a.invoke(<generated>) ~[main/:na]
org.hibernate.LazyInitializationException 이 오류는 영속성 컨텍스트가 종료되어 버려서, 지연 로딩을 할 수 없어서 발생하는 오류 입니다. JPA에서 지연로딩을 하려면 항상 영속성 컨텍스트가 있어야 한다.
보통 트랜잭션 밖에서 조회하면 이런 문제가 발생합니다.
@Transactional로 한방에 해결되었네요.
@Transactional
public ResponseStatusDto resignMembership(Long id, ResignRequest resignRequest) {
User foundUser = userRepository.findById(id).orElseThrow(
() -> new IllegalArgumentException("사용자가 존재 하지 않습니다.")
);
if (foundUser.getUsername().equals(resignRequest.getUsername())) {
userRepository.delete(foundUser);
} else {
throw new IllegalArgumentException("접근할 수 있는 권한이 없습니다.");
}
return new ResponseStatusDto(StatusEnum.USER_DELETE_SUCCESS);
}