Creating table data

Having exported the schema to the database, in add.jsp, persist the Catalog POJO domain model to the database. We will run schemaExport.jsp and the other JSPs in a later section after discussing the JSPs:

  1. Import the classes in the org.jboss.hibernate.model and org.hibernate packages and the org.hibernate.cfg.Configuration class. Create an instance of the Configuration object and configure the hibernate.cfg.xml file as in the schemaExport.jsp.
  2. Create instances of the Catalog class using either the no-argument constructor with the setter methods for the properties or the argument constructor that takes all properties in the manner shown in the following code:
    Catalog catalog = new Catalog();
    catalog.setId(1);
    catalog.setJournal("Oracle Magazine");
    catalog.setPublisher("Oracle Publishing");
    catalog.setEdition("Jan-Feb 2004");
    catalog.setTitle("Understanding Optimization");
    catalog.setAuthor("Kimberly Floss");
  3. Create SessionFactory from the Configuration object using the buildSessionFactory() method. SessionFactory has all the metadata from the mapping and properties files in Configuration. The Configuration object is not used after SessionFactory has been created. Create a Session object from the SessionFactory object using the openSession() method. The openSession method implements JDBC transparently. JDBC connections are obtained from ConnectionProvider internally by Hibernate. We made the Catalog persistent class serializable because a Session object is serializable only if the persistent class is serializable. A Session object is a client interface to Hibernate. The actual persistence to the database is made using a Transaction object. Refer to the following line of code, which puts into action the discussion in this paragraph:
    Session sess = sessionFactory.openSession();
  4. Begin a client session using the beginTransaction() method that returns a Transaction object. A Transaction object represents a global transaction. Refer to the following line of code that summarizes the discussion in this step:
    Transaction tx = sess.beginTransaction();
  5. The beginTransaction() method starts a new underlying transaction only if required; otherwise it uses an existing transaction. Make the Catalog instances associate with the Session object using the save() method, as follows:
    sess.save(catalog);
    sess.save(catalog2);
  6. The save() method does not store the Catalog instances to the database but only adds the POJOs to Session. To store the Catalog instances, invoke the commit() method of the Transaction object in the manner shown in the following code:
    tx.commit();
  7. Optionally output a message to indicate that the data has been added to the database. The add.jsp file 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>
        <%
          Configuration cfg = new Configuration();
          cfg.configure("hibernate.cfg.xml"); 
          Catalog catalog = new Catalog();
          catalog.setId(1);
          catalog.setJournal("Oracle Magazine");
          catalog.setPublisher("Oracle Publishing");
          catalog.setEdition("Jan-Feb 2004");
          catalog.setTitle("Understanding Optimization");
          catalog.setAuthor("Kimberly Floss");
    
          Catalog catalog2 = new Catalog();
          catalog2.setId(2);
          catalog2.setJournal("Oracle Magazine");
          catalog2.setPublisher("Oracle Publishing");
          catalog2.setEdition("March-April 2005");
          catalog2.setTitle("Starting with Oracle ADF");
          catalog2.setAuthor("Steve Muench");
          SessionFactory sessionFactory = cfg.buildSessionFactory();
          Session sess = sessionFactory.openSession();
          Transaction tx = sess.beginTransaction();
          sess.save(catalog);
          sess.save(catalog2);
          tx.commit();
          out.println("Added");
        %>
      </body>
    </html>
..................Content has been hidden....................

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