Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly
more resources

Map a JavaBean Object to a Database
Are you looking for a simpler, more flexible way to map data representation in a Java object? Try building a Hibernate application for a different approach
by Deepak Vohra

January 24, 2006

Hibernate is an open source object/relational persistence and query service for Java. Data representation in an object model is often required for mapping to a relational data model. You can use Hibernate to map data representation in a Java object to a database, and Hibernate supports several databases including DB2, MySQL, Oracle, and PostgreSQL.

ADVERTISEMENT

Using Hibernate, you can map Java datatypes to SQL datatypes. Hibernate generates the required SQL to create, update, and delete database tables, and it is used for generating tables from a JavaBean class and adding, retrieving, updating, and deleting data to the tables. Hibernate is preferred over other database persistence technologies, such as Castor, TopLink, and Entity EJB, because it has less complexity, greater flexibility, an open source architecture, and support for different databases without requiring that vendor-specific SQL code must be provided in the data-access layer. Hibernate also provides classes for Ant build tasks. Let's take a look at using Hibernate 3.0 to develop an object/relational application.

The database configuration properties for developing a Hibernate application are specified in the Hibernate properties file (hibernate.properties) or the Hibernate configuration file (hibernate.cfg.xml). These properties specify the database, the JDBC driver class, and the connection URL for connecting to the database. The hbm.xml mapping file specifies the JavaBean properties and the corresponding database columns to which the properties are mapped. The mapping file also specifies the database table name to which a JavaBean class is mapped. The org.hibernate.tool.hbm2ddl.SchemaExport tool is used to map the mapping file to the database table. Here we'll map a mapping file example—Catalog.hbm.xml—that consists of properties for a journal catalog to an Oracle database table—OE.Catalog.

Bring It to the Table
The Hibernate API classes are required to develop a Hibernate application. Download Hibernate 3.0.5, and extract the files to an install directory (see Resources). Install WebLogic Server 8.1, and then install the Oracle 10g Database (see Resources). (Refer to the Oracle 10g Database documentation to install the Oracle database and create a database instance.) Add the JAR files that are required to generate a Hibernate application to the CLASSPATH environment variable (see Resources). The JAR files for developing a Hibernate application are listed in Table 1.

Let's start by generating a database table from a JavaBean class. A database table can be generated with the hibernate.properties file, which specifies the database configuration parameters, and the .hbm.xml mapping file. If a configuration file (hibernate.cfg.xml) is used to specify the database properties, the properties file isn't required. The .hbm.xml mapping file consists of class definitions that specify the JavaBean properties to be mapped to the database table, the database table's name, and its columns corresponding to the JavaBean properties. The column type, length, not-null, and unique attributes are also specified. Some of the mapping file elements are listed in Table 2.

The mapping file example specifies a JavaBean—Catalog.java—that consists of fields and columns for a journal. In the Catalog.hbm.xml mapping file example:

<?xml version="1.0"?>
  <!DOCTYPE hibernate-mapping 
  PUBLIC
  "-//Hibernate/Hibernate 
    Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/
  hibernate-mapping-3.0.dtd">
<hibernate-mapping> 
<class name="Catalog" table=
  "OE.CATALOG"> 
<id name="id" type="string" 
  column="ID"> 
<generator class="native"/> 
  </id> 
<property name="journal" column=
  "JOURNAL" type="string"/> 
  <property name="publisher" 
    column="PUBLISHER" type=
    "string"/> 
<property name="edition" column=
  "EDITION" type="string"/> 
<property name="title" column=
  "TITLE" type="string"/>
<property name="author" column=
  "AUTHOR" type="string"/>
</class>
</hibernate-mapping>

the <generator class="native"/> element specifies the identifier-generation strategy.




Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home