简体   繁体   中英

Hibernate execute multiple update statements within the same query

I have two update statements. I want to have these two update statements in one query.

String hq1 = "update Table1 set xxxxx";
String hq2 = "update Table2 set xxxxxxxx";
Query query = session.createQuery(hq1);
int result = query.executeUpdate();

Suggest me the right approach. Thanks

The standard SQL syntax doesn't work in this way.
The expected syntax for an UPDATE is :

UPDATE table_name

SET column1 = value1, column2 = value2, ...

WHERE condition;

The column value have to be set with one specific value.
In your case, you want to SET the featured column with a value ( 0 or 1 ) that depends on the matching of the WHERE clause.
You cannot do it.

To achieve your requirement, you could use specific features of your DBMS (some provide a way to go further).
But to do it, you will also have to use native query and not Hibernate query.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM