Updating a table row

In this section, we will update a table row. Perform the following steps to accomplish this:

  1. Create an HQL query String and create a Query object to generate List as discussed for find.jsp.
  2. Obtain the first item in List and modify the publisher value with the setPublisher() method, as follows:
    Catalog catalog = (Catalog) list.get(0);
    catalog.setPublisher("Oracle Magazine");
  3. Create a Transaction object, which represents a transaction with the database, with the beginTransaction() method. Save or update the persistent state of the Catalog object in Session with the saveOrUpdate method. Invoke the commit() method of the Transaction object to save the Catalog instance in the database.
  4. Optionally, output a message to indicate that the update was completed:
    Transaction tx = sess.beginTransaction();
    sess.saveOrUpdate(catalog);
    tx.commit();

    The update.jsp is listed in the following code:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 	pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <%@ page import="org.jboss.hibernate.model.*,org.hibernate.*,java.util.List,org.hibernate.cfg.Configuration"%>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/xml; charset=windows-1252" />
        <title>Export Schema</title>
      </head>
      <body>
        <%
          String hqlQuery = "from Catalog";
          Configuration cfg = new Configuration();
          cfg.configure("hibernate.cfg.xml");
          SessionFactory sessionFactory = cfg.buildSessionFactory();
          Session sess = sessionFactory.openSession();
          Query query = sess.createQuery(hqlQuery);
          List list = query.list();
          Catalog catalog = (Catalog) list.get(0);
          catalog.setPublisher("Oracle Magazine");
          Transaction tx = sess.beginTransaction();
          sess.saveOrUpdate(catalog);
          tx.commit();
          out.println("Updated");
        %>
      </body>
    </html>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset