我尝试将以下代码添加到 spring data jpa 存储库中:
@Query("insert into commit_activity_link (commit_id, activity_id) VALUES (?1, ?2)")
void insertLinkToActivity(long commitId, long activityId);
但是应用程序无法启动,但存在以下异常:
引起:org.hibernate.hql.internal.ast.QuerySyntaxException:意外标记:第 1 行第 59 列附近的 VALUES [插入 commit_activity_link (commit_id, Activity_id) VALUES (?1, ?2)]
我哪里错了?
我必须将
nativeQuery = true
添加到 @Query
@Query(value = "insert into commit_activity_link (commit_id, activity_id) VALUES (?1, ?2)", nativeQuery = true)
使用java对象而不是传递所有参数
@Modifying(clearAutomatically = true)
@Transactional
@Query(value = "insert into [xx_schema].[shipment_p] (gpn,qty,hscode,country_of_origin,created_date_time,shipment_id) "
+ "VALUES (:#{#sp.gpn},:#{#sp.qty}, :#{#sp.hscode} ,:#{#sp.countryOfOrigin}, :#{#sp.createdDateTime}, :#{#sp.id} )", nativeQuery = true)
public void saveShipmentPRoducts(@Param("sp") ShipmentProducts sp);
您应该使用@Transactional和@Modifying以及@Query来使用nativeQuery插入或更新数据。
@Transactional
@Modifying
@Query(value=`enter code here`"insert into commit_activity_link (commit_id, activity_id) VALUES (:commitId, :activityId)", nativeQuery=true)
void insertLinkToActivity(@Param("commitId") long commitId,@Param("activityId") long activityId);