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

XML-to-Database Mapping with Hibernate 3.1

by Deepak Vohra

Hibernate is an open source object/relational persistence and query service for Java. Hibernate's latest version, 3.x, introduces a new feature: XML-to-database mapping, which allows you to to map data representation in an XML document to a database (of which Hibernate supports several—including DB2, MySQL, Oracle, and PostgreSQL). With Hibernate, you map your XML document nodes to database columns. Only an XML mapping file is required to map an XML document to a database table; a POJO is not required. Hibernate generates the required SQL to create, update, and delete database tables. You can also use it togenerate tables from a Hibernate configuration file and add data to a database table from an XML document. You can retrieve database table data as an XML document and update it from that XML document. Further, Hibernate 3.x supports dom4j API for XML processing. Hibernate also provides classes for Ant build tasks. In this tutorial, you'll learn how to develop an XML/relational application with Hibernate 3.1.

The first thing you'll want to do is take a look at the database configuration properties, which are specified in the hibernate.properties file. These files specify the database, the JDBC driver class, and the connection URL you'll use to connect to the database. The mapping file, a hbm.xml file, specifies the XML document nodes and the corresponding database columns to which the nodes are mapped. The database table name to which a Java bean class is mapped to is also specified in the mapping file. You'll use the org.hibernate.tool.hbm2ddl.SchemaExport tool to map the mapping file to the database table. This tutorial will show you how to map an example mapping file (Catalog.hbm.xml), consisting of properties for a journal catalog, to an Oracle database table, OE.Catalog.

Preliminary Setup

You'll need the Hibernate API classes to develop a Hibernate application. Download Hibernate 3.1.2 and extract the files to an installation directory. Install the Oracle 10g database (to do so, install and create the database, refer to Oracle database documentation). Next, add the .jar files required to generate a Hibernate application to the CLASSPATH environment variable (the .jar files are listed in Table 1.

JAR/Zip  File

Description

<Hibernate3.1>/hibernate3.jar

The Hibernate API classes including the org.hibernate.tool.hbm2ddl.SchemaExport  class.

<Hibernate3.1>/lib/dom4j-1.6.1.jar, <Hibernate3.1>/lib/commons-logging-1.0.4.jar,  <Hibernate3.1>/lib/commons-collections-2.1.1.jar, <Hibernate3.1>/lib/ehcache-1.1.jar, <Hibernate3.1>/lib/cglib-2.1.3.jar, <Hibernate3.1>/lib/jta.jar, <Hibernate3.1>/lib/asm.jar, <Hibernate3.1>/lib/antlr-2.7.6rc1.jar

<Hibernate3.1>/lib/jaxen-1.1-beta-7.jar

 

The auxiliary Hibernate classes.

<Oracle10g>/jdbc/lib/classes12.jar or ojdbc14.jar

The Oracle   database JDBC classes.

 

Table 1. Hibernate JAR Files

Table 1 shows <Hibernate> as the directory, C:/Hibernate/hibernate-3.1, in which Hibernate 3.1 is installed.

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.