org.springframework.dao.InvalidDataAccessApiUsageException

2020. 12. 5. 07:00·아카이브/TroubleShooting
반응형

오늘도 어김없이 등장하는 Exception!!

org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query

나를 닮았다고 한다....

증상

Querydsl로 Update, Delete하던 중 에러가 발생하였습니다. 에러가 발생한 코드는 다음과 같습니다.

public Long deleteQuery(Long id) {
    // JpaQueryFactory 사용 시
//    return jpaQueryFactory.delete(store)  
//            .where(store.id.eq(id))
//            .execute();

    // QuerydslRepositorySupport 사용 시
    return delete(store)                
            .where(store.id.eq(id))
            .execute();
}

public Long updateQuery(Long id, String address) {
    // JpaQueryFactory 사용 시
//    return jpaQueryFactory.update(store)
//           .where(store.id.eq(id))
//            .execute();

    // QuerydslRepositorySupport 사용 시
    return update(store)                
            .set(store.address, address)
            .where(store.id.eq(id))
            .execute();
}

 

원인

에러 로그를 살펴보니 문제는 너무나 간단하네요. TransactionRequiredException 대충 보아도 Transaction이 Required 되지 않아서 발생하는 것입니다.

 

해결

update, delete하는 메서드에 @Transactional 을 붙혀줍니다. 수정된 소스는 다음과 같습니다.

@Transactional             // 추가
public Long deleteQuery(Long id) {
    // JpaQueryFactory 사용 시
//    return jpaQueryFactory.delete(store)  
//            .where(store.id.eq(id))
//            .execute();

    // QuerydslRepositorySupport 사용 시
    return delete(store)                
            .where(store.id.eq(id))
            .execute();
}

@Transactional             // 추가
public Long updateQuery(Long id, String address) {
    // JpaQueryFactory 사용 시
//    return jpaQueryFactory.update(store)
//           .where(store.id.eq(id))
//            .execute();

    // QuerydslRepositorySupport 사용 시
    return update(store)                
            .set(store.address, address)
            .where(store.id.eq(id))
            .execute();
}
Tip.
Querydsl로 update, delete하는 방법은 2가지가 있습니다.
첫번째 방법은 QuerydslRepositorySupport 상속받아 update() 메서드를 사용합니다.
  - 내부적으로 JPAUpdateClause() 클래스를 사용합니다.
두번쨰 방법은 JpaQueryFactory를 사용할 수 있습니다.

 

 

 

반응형

'아카이브 > TroubleShooting' 카테고리의 다른 글

[JPA] java.lang.NoSuchMethodException: org.springframework.data.domain.Pageable.<init>  (0) 2020.12.11
[prometheus] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver  (0) 2020.12.08
[junit] Auditor must not be null! Returned by: class com.example.demo.db.LoginUserAuditorAware!  (0) 2020.12.04
[intellij] No candidates found for method call ""  (0) 2020.11.22
Run with --info or --debug option to get more log output. Run with --scan to get full insights.  (0) 2020.11.20
'아카이브/TroubleShooting' 카테고리의 다른 글
  • [JPA] java.lang.NoSuchMethodException: org.springframework.data.domain.Pageable.<init>
  • [prometheus] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
  • [junit] Auditor must not be null! Returned by: class com.example.demo.db.LoginUserAuditorAware!
  • [intellij] No candidates found for method call ""
에디개발자
에디개발자
------ 한발자국씩 성장하자 ------ Github: https://github.com/yongtaelim LinkedIn: https://www.linkedin.com/in/%EC%9A%A9%ED%83%9C-%EC%9E%84-622b69218/
    250x250
  • 에디개발자
    에디블로그
    에디개발자
    • 분류 전체보기 (297) N
      • AI (70) N
        • Claude (29) N
        • ChatGPT (12)
        • Cursor (13)
        • Gemini (10)
        • 트랜드 (5) N
        • 오늘의 AI 업데이트 (1)
      • 개발 트렌드 (39) N
        • 데일리 픽 (30) N
        • 툴 리뷰 (1)
      • 개발자 도구 (12) N
        • 생산성 툴 (4) N
        • 노트 & 지식관리 (2) N
        • 협업 & 이슈 (0)
        • 터미널 & 환경 (1)
        • API & DB (4) N
        • 에디터 & IDE (1)
      • 실험실 (2) N
        • AI 자동화 (0)
        • 자동매매 (1)
        • 블로그 자동화 (1) N
      • 아카이브 (168)
        • MindControl (12)
        • TroubleShooting (16)
        • kotlin ( 아카이브 ) (16)
        • java ( 아카이브 ) (15)
        • spring (6)
        • test (4)
        • message-queue (10)
        • object (22)
        • spring-data (28)
        • spring-batch (7)
        • devops (5)
        • java (5)
        • kotlin (3)
        • database (2)
        • Study (3)
        • Develop (1)
        • Develop Tool (1)
        • Daily Develop (7)
        • Book (5)
  • 인기 글

  • 태그

    AI 업데이트
    OpenAI
    ai 정보
    ChatGPT
    AI 활용
    프로그래밍
    클로드
    Gemini
    queryDSL
    JPA
    ai 코딩
    cursor
    Ai
    에디
    개발자 도구
    개발자
    스터디
    anthropic
    코틀린
    claude
  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
에디개발자
org.springframework.dao.InvalidDataAccessApiUsageException
상단으로

티스토리툴바