In this chapter, we will see how to modify or update the table with desired values. To modify data of a certain attribute of any object, we have to assign new value to it and commit the changes to make the change persistent. Let us fetch an object from the table whose primary key identifier, in our Customers table with ID=2. We can use get() method of session as follows:
Now we need to update the Address field by assigning new value as given below:
Now change name attribute and display the contents using the below code:
Even though the change is displayed, it is not committed. You can retain the earlier persistent position by using rollback() method with the code below:
For bulk updates, we shall use update() method of the Query object. Let us try and give a prefix, ‘Mr.’ to name in each row (Except ID = 2). The corresponding update() statement is as follows:
The update() method requires two parameters as follows:
Three out of 4 rows in the table will have name prefixed with ‘Mr.’ However, the changes are not committed and hence will not be reflected in the table view. It will be refreshed only when we commit the session.