18

I try to add the following code to a spring data jpa repository:

 @Query("insert into commit_activity_link (commit_id, activity_id) VALUES (?1, ?2)") void insertLinkToActivity(long commitId, long activityId); 

But an app can't start with the exception:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: VALUES near line 1, column 59 [insert into commit_activity_link (commit_id, activity_id) VALUES (?1, ?2)]

Where am I wrong?

3 Answers 3

42

I had to add nativeQuery = true to @Query

@Query(value = "insert into commit_activity_link (commit_id, activity_id) VALUES (?1, ?2)", nativeQuery = true) 
Sign up to request clarification or add additional context in comments.

Comments

4

Use java object rather passing all parameters

@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); 

Comments

1

You should use @Transactional and @Modifying along with @Query to insert or update data using nativeQuery.

@Transactional @Modifying @Query(value="insert into commit_activity_link (commit_id, activity_id) VALUES (:commitId, :activityId)", nativeQuery=true) void insertLinkToActivity(@Param("commitId") long commitId, @Param("activityId") long activityId); 

1 Comment

Why @Transactional and @Modifying are required? you can use nativeQueries withou them. Why are you assuming that is needed from the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.