Next, we will update table values with the Hibernate Java application. Create a query to select data, which is to be modified, from the table
String hqlQuery="from Catalog";
Obtain a Session
object from the SessionObject
.
Session sess = sessionFactory.openSession();
Obtain a Transaction
object from the Session
object.
Transaction tx = sess.beginTransaction();
Create a Query
object from the HQL query using the createQuery()
method of the Session
object. Obtain a List
result set with the list()
method. Obtain the JavaBean object to be modified.
Query query = sess.createQuery(hqlQuery); List list = query.list(); Catalog catalog = (Catalog) list.get(0);
As an example, set the value of the publisher
field to "Oracle Magazine"
.
catalog.setPublisher("Oracle Magazine");
Begin a Session
transaction using the beginTransaction()
method of the Session
object.
Transaction tx = sess.beginTransaction();
Update the database table with the saveOrUpdate()
method of the Session
object...