Java 如何使用@query注解在spring数据jpa中进行insert自定义查询
我使用的是mssql和spring数据JPA,我想通过使用自定义Java 如何使用@query注解在spring数据jpa中进行insert自定义查询,java,jpa,spring-data-jpa,jpql,Java,Jpa,Spring Data Jpa,Jpql,我使用的是mssql和spring数据JPA,我想通过使用自定义@Query注释将新记录插入表中 public interface CustomerRepository extends JpaRepository<Customers, String>{ @Modifying @Query("insert into Customers values (?1 , ?2)") public void saveCutomer(int custId, Custome
@Query
注释将新记录插入表中
public interface CustomerRepository extends JpaRepository<Customers, String>{
@Modifying
@Query("insert into Customers values (?1 , ?2)")
public void saveCutomer(int custId, Customer cust);
}
使用JPQL不能做到这一点。您有批量删除和更新功能,但仅此而已 你的选择是: 1)如果确实进行了显式插入,请将查询标记为本机查询(不建议使用,除非您打算使用JPQL不支持的特定于数据库的语法)
2)在您的存储库上使用标准的
保存(实体)
方法,这当然更可取。我尝试了以下方法,其工作@Query(value=“insert into Customers values(?1,?2)”,nativeQuery=true)我没有使用save(实体),因为我使用的是ms sql,我试图将所有数据保存在一列中。你知道怎么做吗??我在UI中使用Angular 8。您必须坚持使用本机。感谢您的时间,我在插入mssql、com.microsoft.sqlserver.jdbc.SQLServerException时遇到以下错误:只有在使用列列表且启用identity_INSERT时,才能为表“Customers”中的identity列指定显式值。
@Modifying
@Query("insert into Customers select ?1 , ?2")
public void saveCutomer(int custId, Customer cust);