I want to execute multiple update statements in the same query in hibernate Hql. like below:
hql = " update Table1 set prob1=null where id=:id1; "
+ " delete from Table2 where id =:id2 ";
...
query.executeUpdate();
in the same executeUpdate
call I want to update records in Table1 and delete records from Table2.
Is that possible?
in the same executeUpdate call I want to update records in Table1 and delete records from Table2.
Is that possible?
executeUpdate()
executes a single update query.
So, no you cannot do it. You have to execute as many update queries as tables to update/delete.
Besides, it makes a code cleaner if you separate queries :
executeUpdate()
It doesn't mean that queries must mandatory be transmited one by one.
Batch Processing is a feature provided by Hibernate to improve performance when you want to execute multiples queries. You have to enable the feature to use it.
hibernate.jdbc.batch_size
property must be set with a suitable value.
If you are undertaking batch processing you will need to enable the use of JDBC batching. This is absolutely essential if you want to achieve optimal performance. Set the JDBC batch size to a reasonable number (10-50, for example):
hibernate.jdbc.batch_size 20 Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator.
Besides from official documentation :
Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator.
Nevertheless, in your case, it will be useless because as Dragan Bozanovic explained you update/delete different tables in your queries. So, it would create as many batch executions as queried tables.
So, you should execute each query individually. Just commit() the transaction when you deem it should be :
hql = "update Table1 set prob1=null where id=:id1;"
...
query.setParameter("id1",...);
query.executeUpdate();
hql = "delete from Table2 where id =:id2";
...
query.executeUpdate();
query.setParameter("id2",...);
..
tx.commit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With