프레임워크/Spring

[Spring] JpaRepository의 delete

pythaac 2022. 4. 4. 19:41

1. 상황

  프로젝트를 진행하면서 아래 이슈에서 ClassCastException이 발생하였습니다. 해당 메서드는 CrudRepository의 delete함수였으며, Post entity를 삭제하고 Post를 반환받는 코드에서 Integer와 충돌한다는 내용의 에러였습니다.

https://github.com/pythaac/mamacoco/issues/3#issuecomment-1087247868

 

[Status=500] TistorySyncExecuter의 deletePost 도중 javax.persistence.TransactionRequiredException · Issue #3 · pythaac/mama

Description [Action] MAMACOCO:8080의 "Let's Sync" 버튼 클릭 [Code] line 176 (TistorySyncExecuter.java) [Stack] SyncController > TistorySyncExecuter#execute > TistorySyncExecuter#dele...

github.com

 

2. 해결

  delete 함수의 반환값은 "delete된 개수" 혹은 "delete된 List"로 설정해야한다고 합니다. 저는 Long형으로 타입을 바꿔서 해당 문제를 해결하였습니다.

 

https://stackoverflow.com/questions/57878392/classcastexception-with-spring-data-jpa-delete-method

 

ClassCastException with spring-data JPA delete method

In my test class, I insert a test product in setUp and delete it in tearDown. During mvn package, I get the following error: java.lang.ClassCastException: java.lang.Integer cannot be cast to com.

stackoverflow.com