이 영역을 누르면 첫 페이지로 이동
일반인의 웹 개발일기 블로그의 첫 페이지로 이동

일반인의 웹 개발일기

페이지 맨 위로 올라가기

일반인의 웹 개발일기

웹 개발과 관련된 모든 이야기

[JPA] org.springframework.dao.DataIntegrityViolationException: could not execute statement 오류 해결

  • 2022.12.19 10:31
  • 에러 정리/JPA
반응형

에러 상황

JPA를 사용하여 개발하던 중 먼전 delete 호출하고 insert를 호출하는 로직 개발 중에 DataIntegrityViolationException, DataException 에러가 발생했다.

 

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a];
nested exception is org.hibernate.exception.DataException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:280)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:566)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:743)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711)
at jdk.internal.reflect.GeneratedMethodAccessor79.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:128)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at jdk.proxy2/jdk.proxy2.$Proxy126.commit(Unknown Source)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:152)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258)

 

오류 내용을 살펴보면 데이터 무결성 관련 위반 에러로 확인할 수 있었다.

데이터를 삭제 후 다시 insert를 하려고 하니 발생한 에러였다.

testRepository.deleteAll();

testRepository.saveAll(testList);

 

원인

Execute all SQL (and second-level cache updates) in a special order so that foreign-key constraints cannot be violated:

  1. Insert, in the order they were performed
  2. Updates
  3. Deletion of collection elements
  4. Insertion of collection elements
  5. Deletes, in the order they were performed

외래 키 제약조건을 위반하지 않기 위해 모든 SQL문이 위와 같은 순서로 동작한다.

insert가 가장 먼저 실행되고 마지막으로 delete가 실행되기 때문에

delete 후 insert를 하게 되면 에러가 발생하게 된 것이다.

 

해결방법

데이터를 삭제하고 flush 호출한 뒤에 insert를 호출하면 된다.

testRepository.deleteAll();
testRepository.flush();
			
testRepository.saveAll(testList);

 

deleteAll, flush, saveAll 순으로 변경했더니 쿼리가 정상적으로 실행되는 것을 확인할 수 있었다.

반응형

댓글

이 글 공유하기

  • 구독하기

    구독하기

  • 카카오톡

    카카오톡

  • 라인

    라인

  • 트위터

    트위터

  • Facebook

    Facebook

  • 카카오스토리

    카카오스토리

  • 밴드

    밴드

  • 네이버 블로그

    네이버 블로그

  • Pocket

    Pocket

  • Evernote

    Evernote

다른 글

다른 글 더 둘러보기

정보

일반인의 웹 개발일기 블로그의 첫 페이지로 이동

일반인의 웹 개발일기

  • 일반인의 웹 개발일기의 첫 페이지로 이동
반응형

검색

메뉴

  • 홈
  • 태그
  • 방명록

카테고리

  • 분류 전체보기 (47)
    • 사이드 프로젝트 (3)
      • 크롤링 (2)
    • 개발 이야기 (18)
      • MSA (7)
      • Spring Boot (3)
      • JPA (0)
      • Docker (1)
      • Javascript (2)
      • AWS (Amazon Web Services) (5)
      • Jenkins (0)
    • Database (4)
      • PostgreSQL (2)
      • MySQL (1)
      • Oracle (1)
    • 에러 정리 (4)
      • Docker (1)
      • JPA (1)
      • Python (1)
      • PostgreSQL (1)
    • 운영체제 (3)
      • Linux (3)
    • 게임 (8)
      • 마인크래프트(Minecraft) (2)
      • 팰월드(PalWorld) (6)
    • 워킹홀리데이 (6)
      • 일본 워킹 홀리데이 (6)

공지사항

인기 글

최근 글

정보

흔하디흔한개발자의 일반인의 웹 개발일기

일반인의 웹 개발일기

흔하디흔한개발자

블로그 구독하기

  • 구독하기
  • RSS 피드

방문자

  • 전체 방문자
  • 오늘
  • 어제

티스토리

  • 티스토리 홈
  • 이 블로그 관리하기
  • 글쓰기
Powered by Tistory / Kakao. © 흔하디흔한개발자. Designed by Fraccino.

티스토리툴바