I have 2 tables, salesOrder with column "id", "customerId", "textReview(boolean)" and table SalesOrderline with column "salesOrderId", "productId". i want to change boolean data in textReview column. I got data from the client in the form is customerId and productId.
Native Query SQL that I made like this.
update sales order set text_review = true where (select id from salesorder where customer_id =?) in (select salesorder_id from salesorderline where product_id =?)
Is the SQL query correct?
I'm confused how to execute this query using jpa or hibernate in spring boot.
In SQL should be something like:
update salesOrder
set text_review = true where customer_id =?
and id in (select salesorder_id from salesorderline where product_id =?)
In JPA I don't think you need the select statement.
You can execute the native SQL query as is also in JPA/Hibernate (as long as the query itself is correct) by the following:
@PersistenceContext(unitName = "<pu name>")
EntityManager em;
em.createNativeQuery("<native sql query>")
.setParameter(1, customerId)
// other parameters as needed
.executeUpdate();
in case it is an UPDATE/DELETE query
SELECT native queries can also be executed via JPA but then you will usually want to get a result list out of the query, instead of executeUpdate()
you can write the native query as below.
@Modifying
@Transactional
@Query(value = "update salesOrder set text_review = true where customer_id =:customerId and id salesorder_id (select salesorder_id from salesorderline where product_id =:productId)" , nativeQuery =true)
void updateTextView(@Param("customerId")String customerId, @Param("productId")String productId);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.