advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : XML-to-Database Mapping with Hibernate 3.1 :

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();

Home / Articles / XML-to-Database Mapping with Hibernate 3.1 / 1 / 2 / 3 / Next Page

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs