Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a native query in jpa for update?

I write a query to update a customer record as native query in jpa but unable to get response. it giving me some irrelevant responses

@Query(value = "UPDATE CUSTOMER SET NAME = :name, PHONE = :phone")
Customer updateWithNameAndPhone(@Param("name") String name, @Param("phone") String phone)
like image 614
Hemraj Malhi Avatar asked Sep 16 '25 05:09

Hemraj Malhi


1 Answers

To show that the query updates the database, use the @Modifying annotation. To indicate that the query is a native SQL query, provide the nativeQuery = true argument. The updated customer object is not required as a return value, hence the void return type is used. You can switch the return type to Customer and adjust the function implementation if you wish to get the updated customer object.

@Query(value = "UPDATE CUSTOMER SET NAME = :name, PHONE = :phone WHERE CUSTOMER_ID = :customerId", nativeQuery = true)
@Modifying
void updateCustomerWithNameAndPhone(@Param("name") String name, @Param("phone") String phone, @Param("customerId") Long customerId);
like image 179
Chander Parkash Avatar answered Sep 18 '25 17:09

Chander Parkash