Hibernate query language also supports bulk update and bulk delete. For example, if you want to increase the price of all the products by $1, then you can use this feature. To demonstrate the same. Let's go and comment out this and write a new function called increment product price by and then I'm going to supply what is the amount by which you want to increment, I'm going to supply say, for example, one dot zero saying that increment the product price by $1. We don't have this function, so I'm going to create a new one and then change this to ANC maybe a variable for representing increment price. Now in order to increment this, I can say here string SQL equals to update p capital product because it's the name of the class, not the name of the table.
Set unit price equals to the current unit price. Plus probably a parameter here called ANC price. And this ANC that I received as function argument must be supplied over here, we simply say your QR y equals to session dot create query. This time, we don't have to really worry about the type of the data that we want, because we're not going to retrieve any data from the DB. This is my SQL. And I simply create a query object just like that.
I can say query dot set parameter, and then the parameter name is ANC price, the value happens to be a CNC. And then I have to say your query dot execute update. This returns the number of rows affected. So I can always type your count equals to press Control one or Command one to generate that variable. And then I can type your total of count products. Update Before we run this, there is one other thing that we have to do, we have to do this in a transactional boundary.
So I'm going to say here somewhere in the beginning session dot begin transaction. And after executing, I'm going to say here, session dot, get the current transaction and then commit the same. Ideally, we should surround all these statements inside a try block. And then in the catch block, we should also do a rollback. before running the code. Let's go and see what prizes we have.
For the products. first few of them you can see $18 $19 and $10. If I go and run this program, now I should see that there are 77 products updated. If I go here and then rerun this, you should see that it is $19 $20 and $11. If I run this once more, again, it's going to be updated with the new price. I'll come back here and rerun this.
It changes from 19 2011 to 2021 and 12