1

I am using mssql and spring data JPA, I want to insert new records to a table by using custom @Query annotation.

public interface CustomerRepository extends JpaRepository<Customers, String>{


    @Modifying
    @Query("insert into Customers values (?1 , ?2)")
    public void saveCutomer(int custId, Customer cust);

}

Its giving error,

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting OPEN, found 'values' near line 1, column 23 [insert into Customers values (?1 , ?2)]

I tried below also, same error.

@Modifying
    @Query("insert into Customers select ?1 , ?2")
    public void saveCutomer(int custId, Customer cust);

1 Answer 1

2

You don't do that using JPQL. You have batch DELETE and UPDATE but that's it.

Your options are:

1) Mark the query as native if you really make explicit insert (not recommended unless you intend to use some database-specific syntax not supported by JPQL.

2) Use standard save(Entity) method on your repository which is of course preferable.

Sign up to request clarification or add additional context in comments.

3 Comments

i tried the below, its working @Query(value="insert into Customers values (?1 , ?2)", nativeQuery = true) i am not using save(Entity) because i am using ms sql and i am trying to keep all data in a single column. any idea, how to do this?? I am using Angular 8 for the UI.
You have to stick with the native then
thanks for your time, i am getting below error while inserting into mssql, com.microsoft.sqlserver.jdbc.SQLServerException: An explicit value for the identity column in table 'Customers' can only be specified when a column list is used and IDENTITY_INSERT is ON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.