Tuesday, November 6, 2007



HIBERNATE


1. What is Hibernate?


§ Object/Relational (OR) Mapping Tool.
Object-relational mapping is, mapping object-oriented programming objects to relational databases managed by MYSQL, Oracle, DB2, Sybase, and other relational database managers (RDBMS).
§ Helps in Transparent Persistence.
Object-relational mapping products have the ability to directly manipulate the data stored in the relational database using object oriented programming. This is in contrast to the database sub-language used by Embedded SQL or call interface used by JDBC or ODBC.
§ An object-relational mapping product is likely to reduce programmer code and, through caching, improve performance.


2. Why use Hibernate?

Hibernate provides a lot of advantages over the traditional method of interacting with the database.
§ Ease of interaction with the Database:
Hibernate maps plain old java objects(that have simple getter/setter methods for the attributes in the table) to the tables already created in the database using the .hbm.xml files (also called mapping files), provides queries to insert, update, delete etc. data in the tables though it does not help in creating tables.
§ JDBC Calls not required:
A hibernate. properties file, which has details about the database, needs to be created. While configuring Hibernate, this properties file is looked up for connecting to the database without the user having to make the JDBC calls and thereby avoids the hard coding of IP, port, username, password etc in the code. The driver for the database needs to be included.
§ The Hibernate Query Language (HQL):
Database related queries should be written in this language and Hibernate converts it to a query that is supported by the underlying Database.
Advantages of using HQL:
o HQL facilitates writing database independent queries. These are converted to SQL queries specific to the underlying database at runtime. Learning SQL specific to the database is not a must.
o Makes the application program very loosely coupled with the underlying database.


3. Tools that support Hibernate:

XDoclet
AndroMDA
Middlegen
Spring FrameWork
All these tools help us generate the objects and/or mapping documents used in Hibernate.

4. Using Hibernate:

To use Hibernate the following documents need to be in place.
Note:-Samples of these files can be found in the attachment named source.zip
§ The jar files specific to Hibernate. (details are given below)
§ Hibernate.properties file.
§ Bean Class which is a POJO (Plain Old Java Object) which represents the Table.
§ Mapping Document which is a XML.
§ The Application Program.


4.1 The Jar files:

Having obtained the Hibernate package, include the following jar files in the Classpath. The jar files are:
§ hibernate2.jar ;
§ ehcache-0.7.jar ;
§ cglib-full-2.0.1.jar ;
§ xerces-2.4.0.jar ;
§ dom4j-1.5.jar ;
§ jta.jar ;
§ j2ee.jar ;
§ commons-collections-2.1.jar ;
§ mysql-connector-java-3.0.14-production-bin.jar ;
Most of these jar files can be found in the lib folder of the package. MySQL-connector and J2EE .jar files need to be procured. MySQL Connector is being used since the DB used in the application is MYSQL. The driver for the MySQL Database can be found in this jar. Hence depending on the Database that the application demands, corresponding driver needs to be procured.


4.2 Table in the Database:

Tables need to be created before running the application. Hibernate does not help in creating tables. Its main purpose is to make the transient data persistent with least trouble. The table that is used in the example has the following fields:

§ SerialNo: - Serial Number, which is the primary key and is an auto increment.
§ FirstName:- First name of the employee
§ LastName: - Last name of the employee.

Note: - The database that is used is MySQL. The database is not on the local machine. It is in the network.

The DDL used for creating the Table is:

“Create table tblEmpDetails (
SerialNo int primary key auto_increment,
FirstName varchar (15),
LastName varchar (15));”


4.3 Hibernate. properties file:

Databases usually have various details linked to them. All these are reflected in the file hibernate.properties. This file holds details like:
§ IP Address of the Database.
§ User ID.
§ Password
§ Dialect( This is specific to the database , and can be obtained from the hibernate API)
§ Other details like connection-pool, show-sql, etc.
This file comes with the Hibernate package in the folder src. It needs to be modified to suit the applications requirements.


4.4 Defining Persistent Domain Object:

This is basically the bean class that has simple getter/setter methods for the attributes that are specified in the table. Inorder to persist data, the bean class is instantiated and using the setter methods, data is set. The object is then saved to the database. The Persistent Domain object in the example is EmpDetails.java. Mapping of the bean class to the table happens in the .hbm.xml file also called the mapping file explained next.


4.5 Hibernate mapping File:

Mapping files in Hibernate are basically XML files. The various tags and their functions are given below. An xml file corresponding to the bean and the table is given in the attachment (EmpDetails.hbm.xml). The DTD-(Document Type Definition)-for the xml file is predefined and can be found in the hibernate package in the folder src\net\sf\hibernate. While compiling the XML document, the application searches for the DTD in the URL specified. If it fails to locate it then it searches for it in the hibernate package itself. The various tags that were used are given below.

No comments: