I made a change in the blogger configuration to ease the later work when blogging. It is possible that older entries are not correctly formatted.

Thursday, 13 December 2007

First Start to Hibernate

In the series of new technologies which I should get my hands on and about which I wish to get an overview Hibernate is an important candidate.

First of all Hibernate is an Object/Relation Mapping framework, this means that it is used to create maps between object oriented structures and relational databases. The main goal is to keep the object in a running system as persistent as possible with the data present in the database in order to ensure consistency in the data. Another important point is also the efficiency of querying and the optimized use of resources.

In Hibernate this is managed through the use of a mapping defined using XML configuration files. The mappings are stored in a file looking like that:

<?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>
[...]
</hibernate-mapping>

A mapping as the following shape:

<class name="myaddressbook.model.Person" table="PERSONS">
....
</class>
where the name is the java name of the class, whereas PERSONS is th name of the relational table of this class.

the fields of a class Person of the sort:

package myaddressbook.model.Person;

class Person {

Long id;
String name;
Date birthday;

// plus the corresponding getters/setters
}
would look like:

<id name="id" column="PERSON_ID">
           <generator class="native"/>
       </id>
       <property name="birthday" type="timestamp" column="BIRTHDAY"/>
       <property name="name"/>


finally the complete file should be under: person.hbm.xml at the place where the file is found.

For the configuration of Hibernate, it is important to set the information about the database. For this, there are different possibilities:

  • a properties file
  • an XML configuration files
  • or using Java to set the information