spring/error

[에러 해결] No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call

얼킴 2023. 2. 14. 13:56

문제

처음 JPA 학습을 시작할 때 Entity 작성 -> Repository 작성 -> test 작성 순으로 코드를 작성하다보면 test 작성에서 다음과 같은 에러를 마주칠 때가 있다.

org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call

에러 내용은 Entity Manager가 현재 쓰레드에 존재하지 않아서 Persist call을 호출 할 수 없다는 것이다.

 

원인

결론부터 얘기하자면 @Transactional 어노테이션을 빼먹어서 발생한 문제였다.

JPA는 transaction위에서 작동한다. 

Repository작성 단계에서 영속 작업을 위한 persist()메소드를 호출했지만 가능한 transaction이 존재하지 않기 때문에 위와 같은 에러가 발생했다.

 

해결

클래스에 @Transactional 어노테이션을 까먹지 말고 선언해주면 된다.