Home arrow Java arrow Page 3 - Introducing Hibernate

Introducing Hibernate

Databases are complex to implement. This is especially true when components of a database based on two different approaches (i.e. object oriented vs. relational) must interact with each other. Object Relational Mapping (ORM) frameworks can act as mediators between the two approaches. Hibernate, the topic of this article, is an ORM framework for Java.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 77
December 06, 2005
  1. · Introducing Hibernate
  2. · Object Relational Mapping: What is it?
  3. · Getting Started With Hibernate
  4. · Hibernate in the Real World

print this article

Introducing Hibernate - Getting Started With Hibernate
(Page 3 of 4 )

ORM frameworks release the developer from the constraints of thinking in terms of relation while working with classes. There are many frameworks that come under this category. What makes Hibernate different from others is its simplicity and flexibility. To make one’s application Hibernate enabled, there are three prerequisites that need to be fulfilled, which are:

1. Persistence Class:

The persistence class is a Plain Old Java Object or POJO model. A POJO is comparable to a JavaBean, having getters and setters to access its properties that are the instance variables. The persistence class has the following characteristics:

a. It is the Object Oriented blueprint of the table to be persisted.

b. The attributes of the table become the instance variable of the persistence class.

c. The data type of the instance variables is the domain of the attributes.

d. The object of the persistence class represents the row of the table.

 2. Mapping file:

The mapping file is an XML file that contains detailed mapping between the persistence class and the table it represents. The required elements of this XML file are:

     a. hibernate-mapping:

         Hibernate-mapping is the root element enclosing all other elements.

     b. class:

Class is used to map the table name to the persistence class. The name attribute  is used to specify the class name, and the table attribute is used to specify the table which the class represents. For example, to map a table named ORDERS to a persistence class with a fully qualified name of com.someorg.persist.Order the class element would be:

<class name=”com.someorg.persist.Order” table=”ORDERS”>…</class>

       c. id:

This element is used to map the primary key of the table to an instance variable of the class. The column child element of the id can be used to map the primary key to the corresponding variable. Whether the value of the primary key can be automatically generated is declared here. The generator element can be used to tell Hibernate whether a class would be used to auto generate the ids, or that the ids are being assigned by the application. The most common approach is assigned. For example:

      <id name="id" type="string" 
        <column name="id" sql-type="char(32)" 
        <generator class="assigned"/>

tells Hibernate that the name of the primary key is “id” which is mapped to the  instance variable “id”. The data type of the variable is “string” which is mapped to the column “id” having Domain as “char(32)” represented by the sql-type attribute. The generator’s class attribute contains the value “assigned” meaning that the application itself will provide the value of the primary key.

     d. property:

This element, along with its child element column, maps the other attributes (or columns) to the instance variables of the persistence class. The name attribute of the property contains the name of the variable as a value. The name attribute of the column element contains the name of the column to which the instance variable is mapped. The length and sql-type attributes point to the length and data type of the column. To cite an example:

       <property name="name">
            <column name="name" sql-type="char(255)"

maps the Column “name” to instance variable “name”.

3. Hibernate Configuration file:

This file can be called the entry point to a Hibernate enabled application. The reason is that it contains the configuration that controls the communication with the underlying database. It is here that all the mapping files being used by the application are declared. The hibernate-configuration element forms the root element. The most common elements used in this XML file are:

a. property:

The name attribute of the property element can be used to configure various parameters. In the terminology of Hibernate, these parameters are known as property parameters. Database server type (MySQL, Oracle, and so on), dialect class, URL of the database, username/password et. al. are the various property parameters. To put it in code:

<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:data/tutorial</property>
<property name="connection.username">sa</property>

     b. mapping:

It is through the mapping element that all the mapping files, also called hbm files, to be used by the application are declared. The resource attribute is given the value of the path to the hbm file. To elucidate:

<mapping resource="Event.hbm.xml"/>

That is all one needs to get started with Hibernate. In the next section I will put it all together to provide a view into the real world of Hibernate.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials