Suppose we want to update the data stored in the DB. Now, what we can do is we can retrieve the data using the get function. And then we can make the changes inside a transaction boundary, and they automatically get updated to the DB. So to demonstrate the same, let's go and create a new Java class. I'm going to call this as 03 package. The program is going to be called as main.
I also want the main function over here. It's full screen the same. I'm going to paste a small snippet here, where I've got a factory object here and then a session object here session is closed and factory's also closed. Let's do an import by pressing Command Shift o or Ctrl Shift on Windows. And in case of a name conflict, it's going to ask me so I have to select our hibernate session. And then everything is okay.
Now we have a session object here. Let's say we want to retrieve the data from the DB and then make some changes. So first, I'm going to begin a trial section by calling session dot begin transaction. And then I will say, session dot get category dot class from my package, and then the ID is one. And then I'm going to assign that to a variable called C one and then create the variable of type called category. Now remember, the transaction has been started, which means by the time I do a transaction commit, all the changes to this object c one will be monitored and effectively updated to the DB as well.
For example, if I type your C one dot set description to something else, like for example, Cola, coffee, tea, etc. And then if I say here, session dot get transaction dot commit. Now you'll see that at the time of commit, it's going to fire an SQL UPDATE statement. Now how does it happen at this time, see one is considered as a persistent object, which means hibernate manages the state of this particular semen. So it constantly observes the changes done to the C one, and writes appropriate SQL statements here, such as delete, or update statements back to the DB. Since the data that we have given here is something that is different than what is there in the database table, it knows that c one has become 30, and it will automatically write an SQL UPDATE statement.
So if I run the program second time, since the DB also contains the same text, and I'm assigning the same text once again, hibernate now consider that this C one is no longer dirty, and the SQL update will not be sent. So just to demonstrate the same, let's save this and run this and we will see that it sends an SQL UPDATE statement this time here and it is also updated in the DB so I can run this command again and you will see that it is To call our coffee, tea, etc. Whereas if I run this code once more, you will not see the actual SQL UPDATE statement. There's only a select statement because of the get command here. But there is no update going on here because this exactly matches the data in the database table. Suppose you have made the changes after the session has been closed, and you want to reopen the session, and then update that to the DB.
If you want to do that. Then you can use the update function from hibernate session. For example, if I come down over here, and then you start a new session by calling session equals to factory dot open session, so let's call it as new session. And then using this session if I want to send an update statement, because I change the value over here. Now if you want to send this details, you can always say your session dot begin transaction And then save your session dot update, and then supplies T one because this is the change to data node because at this time c one has nothing to do with the session object it has been disconnected. We call this as a detached object now, but then I'd say session dot get transaction dot commit.
And I will see that this will generate an SQL UPDATE statement because you have given an update function called over here, which means that the session dot update always makes this see one as a dirty object. And session dot get transaction commit will report that as an SQL UPDATE statement. So just to ensure that this works, I'm going to save this run this and you will see that SQL UPDATE statement is fired irrespective of whether there is any change or not. Someone run this again to verify the same and you should see that there is an SQL update again