Adding Database Table Data from an XML Document
In this section, you'll develop a Java application to add data from an an XML document to the database table you generated in the previous section. You'll be using the data from catalog.xml, listed in Listing 2.
The Java application integrates the mapping file, and the properties file for database persistence. Copy the mapping file Catalog.hbm.xml to the same directory as the hibernate.properties file directory. Add the directory to the CLASSPATH variable. In the Java application, import the Hibernate API classes that are in the org.hibernate package and the dom4j classes as shown in Listing 3.
org.hibernate.Session is the main runtime interface between a Java application and Hibernate. You'lll use it to add, retrieve, update, and delete XML data in the database table (Catalog). Obtain a Session object from a SessionFactory. The SessionFactory interface provides openSession() methods to create a database connection and open a session on the connection, or open a session on a specified connection. The org.hibernate.cfg.Configuration class is used to specify configuration properties and mapping files to create a SessionFactory. Create a Configuration object object:
Configuration config=new Configuration();
Add the mapping file, catalog.hbm.xml, to the Configuration:
config.addFile("catalog.hbm.xml");
The mapping file, Catalog.hbm.xml, and the JDBC properties file are in the same directory as the Hibernate application and they get configured along with the Configuration object. Create a SessionFactory object:
SessionFactory sessionFactory=config.buildSessioFactory();
Next, add data to the database table created with the SchemaExport tool. Obtain a Session object from the SessionFactory object:
Session sess =sessionFactory.openSession();
Obtain a Transaction object from the Session to add data to the database table:
org.hibernate.Transaction tx = sess.beginTransaction();
Start a session with entity mode DOM4J. The secondary session has the same connection, transaction, and context characteristics as the primary session:
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Create a SAXReader to parse the XML document that is to be persisted to the database. Parse the XML document catalog.xml using read(File) method:
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("c:/Hibernate/catalog.xml"));
Obtain a List of catalog nodes in the Document object and create an Iterator object for the List object:
List list = document.selectNodes("//catalog");
Iterator iter = list.iterator();
Iterate over the List object and retrieve node objects from the List. Save the node objects in the database using the save(String entityName, Object object) method. The save() method does not save the catalog node objects to the database. The flush() method is used to synchronize the database with the XML document nodes in the Session object:
while (iter.hasNext()) {
Object catalog = iter.next();
dom4jSession.save("Catalog", catalog);
}
Subsequently, flush the session, commit the transaction, and close the session:
session.flush();
tx.commit();
session.close();
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|