我看到這些鏈接彈簧數據JPA插入使用@Query&@Modifying而不使用nativeQuery或保存()或saveAndFlush()
- How to use JPA Query to insert data into db?其使用nativeQuery =真
- How to insert into db in spring-data?這表明使用內置的保存方法(也有saveAndFulsh()方法)
我下面的例子是:
人是一個簡單的實體W/3場「龍ID,字符串名稱,整數年齡」,並映射到相應的Person表W持續高於/ 3列)
@Repository
public interface DualRepository extends JpaRepository<Dual,Long> {
@Modifying
@Query(? - what goes here - ?)
public int modifyingQueryInsertPerson(@Param("id")Long id, @Param("name")String name, @Param("age")Integer age);
}
有沒有辦法通過只使用@Query & @Modifying做插件(即不使用原生SQL查詢& nativeQuery = true,或者save()或saveAndFlush()?
經過進一步測試,它*不能在MS-SqlServer中工作,因爲MS-SqlServer不提供系統虛擬表(如Oracle中的DUAL)。 它*在Oracle和MySQL中工作,因爲它們都有一個DUAL虛擬表。 因爲HQL在SELECT之後需要FROM,所以只有底層數據庫有一個虛擬表(如Oracle中的DUAL)時,上述方法才能用於INSERT。 –