[Spring Boot][오류해결] Modifying queries can only use void or int/Integer as return type

2022. 2. 20. 19:00·JAVA/오류해결

 

Resolved [org.springframework.dao.InvalidDataAccessApiUsageException: Modifying queries can only use void or int/Integer as return type! Offending method: public abstract com.example com.example.repository.myRepository.mySave(int,int,java.lang.String); nested exception is java.lang.IllegalArgumentException: Modifying queries can only use void or int/Integer as return type! Offending method: public abstract com.emaxple com.example.repository.myRepository.mySave(int,int,java.lang.String)]

 

위 오류는 네이티브 쿼리를 사용할 때 발생하는 오류이다.

네이티브 쿼리를 사용하면 JPARepository의 쿼리를 직접 조작하면 @Modifying 어노테이션을 붙이게 되는데

이 어노테이션을 사용하면, void로 리턴 값이 없거나 int만 리턴할 수 있다. (만약 int를 리턴한다면 업데이트된 행의 수의 리턴해준다.)

 

 

 

문제의 코드

public interface MyRepository extends JpaRepository<MyClass, Integer> {
	
	@Transactional
	@Modifying
	@Query(value="INSERT INTO MyClass(param1, param2) VALUES(?1, ?2)", nativeQuery=true)
	MyClass mySave(int param1, String param2); 
}

 

 

해결된 코드

public interface MyRepository extends JpaRepository<MyClass, Integer> {
	
	@Transactional
	@Modifying
	@Query(value="INSERT INTO MyClass(param1, param2) VALUES(?1, ?2)", nativeQuery=true)
	int mySave(int param1, String param2); // 또는 void 설정
}
'JAVA/오류해결' 카테고리의 다른 글
  • java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package
  • [SpringBoot][오류해결] The dependencies of some of the beans in the application context form a cycle
  • [Spring Boot][오류해결] Executing an update/delete query
  • [Spring Boot][오류해결] Parameter 0 of constructor in ~~ required a bean of type ~~ that could not be found.
gakko
gakko
좌충우돌 개발기
  • gakko
    MYVELOP 마이벨롭
    gakko
  • 전체
    오늘
    어제
    • 분류 전체보기 (203)
      • Spring (23)
        • Spring (10)
        • Spring Boot (7)
        • Spring Security (1)
        • Hibernate (4)
      • Test (3)
      • 끄적끄적 (6)
      • 활동 (35)
        • 부스트캠프 (23)
        • 동아리 (3)
        • 컨퍼런스 (3)
        • 글또 (5)
        • 오픈소스 컨트리뷰션 (1)
      • 디자인패턴 (0)
      • Git & GitHub (22)
        • Git (13)
        • Github Actions (1)
        • 오류해결 (5)
        • 기타(마크다운 등) (3)
      • 리눅스 (6)
        • 기초 (6)
        • 리눅스 서버 구축하기 (0)
      • Infra (2)
        • Docker (1)
        • Elastic Search (0)
        • Jenkins (1)
        • AWS (1)
      • MySQL (7)
        • 기초 (6)
        • Real MySQL (1)
      • 후기 (3)
        • Udemy 리뷰 (3)
      • CS (26)
        • 웹 기본지식 (0)
        • 자료구조 (13)
        • 운영체제 OS (12)
        • 데이터베이스 (1)
        • 시스템 프로그래밍 (0)
        • 기타 (0)
      • Tools (1)
        • 이클립스 (1)
        • IntelliJ (0)
      • 프로젝트 (1)
        • 모여모여(부스트캠프) (1)
      • JAVA (32)
        • Maven (6)
        • 오류해결 (11)
        • 자바 클래스&메소드 (1)
        • JSP & Servlet (12)
      • Javascript (5)
        • 기초 (3)
        • React (2)
      • Python (28)
        • 파이썬 함수 (9)
        • 알고리즘 문제풀이 (16)
        • 데이터 사이언스 (2)
        • 웹 크롤링 (1)
      • 단순정보전달글 저장소 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 우진님
  • 공지사항

  • 인기 글

  • 태그

    부스트캠프 멤버십
    스프링부트
    MySQL
    GitHub
    파이썬
    운영체제
    자바
    스프링
    부스트캠프 7기
    Python
    jsp
    java
    부스트캠프
    Git
    Spring
    자바스크립트
    알고리즘
    웹개발
    os
    오류해결
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.0
gakko
[Spring Boot][오류해결] Modifying queries can only use void or int/Integer as return type
상단으로

티스토리툴바