網頁

2019/11/13

Spring Data JPA @Query nativeQuery ORA-03001: unimplemented feature 錯誤

今天在Spring Data JPA的Repository介面定義的@Query的native query方法執行INSERT INTO..SELECTSQL時,出現java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement錯誤。

下面是發生錯誤的DemoRepository

DemoRepository

package com.abc.repository;

import com.abc.entity.OneEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface DemoRepository extends JpaRepository<OneEntity, Long> {

    @Query(value =
            " INSERT INTO T_ONE (T_ONE_COL_1) " + 
            " SELECT T_TWO_COL_1 FROM T_TWO ", nativeQuery = true)
    void demoQuery();

}

造成錯誤的原因是執行的SQL為新增,也就是有異動到資料庫,所以必須要另外加上@Modifying annotation即可解決,修改後如下。

DemoRepository

package com.abc.repository;

import com.abc.entity.OneEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface DemoRepository extends JpaRepository<OneEntity, Long> {

    @Modifying
    @Query(value =
            " INSERT INTO T_ONE (T_ONE_COL_1) " + 
            " SELECT T_TWO_COL_1 FROM T_TWO ", nativeQuery = true)
    void demoQuery();

}


沒有留言:

張貼留言