今天在Spring Data JPA的Repository介面定義的@Query
的native query方法執行INSERT INTO..SELECT
SQL時,出現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();
}
- Spring Data JPA @Query nativeQuery org.hibernate.exception.SQLGrammarException: could not execute query 錯誤
- Spring Data JPA org.hibernate.AnnotationException: No identifier specified for entity 錯誤原因
- Spring Data JPA SQLSyntaxErrorException: ORA-00904: 無效的 ID invalid identifier
- Spring Data JPA org.springframework.orm.jpa.JpaSystemException: identifier of an instance of Entity was altered from x to y錯誤
沒有留言:
張貼留言