rad!ance

Homepage and Weblog of Fleur Jeanquartier

Hibernate – not only bears do

Hibernate is not only a general function of operating systems to suspend themselves and does not only describe a specific state in which animals rest in winter, but also describes an enhanced version of the Data Source Layer in context of a Java Application Architecture.

In general, the data source layer is responsible for the communication with the (in most cases relational) database management systems. This layer can be implemented using the Open Source Project called Hibernate, which is an open-source product developed in Java. Domain objects are then mapped with the data sources in a Object-To-Relational mapping, and the so created java objects can be further used in more abstract layers. This kidn of procdure is called “Object Relation Mapping” (ORM).

Hibernate grants such ORM features such as the separation of dialogs from business logic and the logic from the persistence mechanism to easily apply changes to one part without influencing the other parts. Therefore ORM helps Java Developers to focus more on the business logic than on repositories, data access etc.

Hibernate is also interoperable with any JDBC compliant database and supports many popular SQL-dialects including Oracle, MSSQL, PostgreSQL, MySQL, Firebird and many more.

Stacks of Hibernate (for Java) (* Source: http://www.hibernate.org/)

To use Hibernate, you need to the following steps:

  • first of all you need to create the java objects itself (you can either do this by firstly creating a the class files of the objects and create the data source afterwars, or just start with existing database tables and create your objects by a reverse engineering,
  • create a hibernate configuration file (hibernate.cfg.xml)
  • create a hibernate mapping file for each java object that has to be persisted (className.hbm.xml – the DTD file is included in hibernate3.jar),
  • further add all the class mappings to the <session-factory>-element of the configuration file,
  • then create a class only for starting Hibernate (see also caveatemptor.hibernate.org for an example),
  • and finally a test case or respectively the main application itself for using the persitence classes.

Regarding the use of Hibernate there is a special plugin for Eclipse called “Hibernate Tools” that grants many extended functions for setting up Hibernate for your web application within Eclipse. The Hibernate Tools provides

  • a Mapping Editor for easily mapping relational data directly to XML files,
  • a Wizard for setting up a configuration file,
  • possibilites of automatic code generation,
  • a Hibernate console for a Hibernate development perspective with a special dynamic view of the persistence calsses,
  • code completion for HQL inside Java code and additional problem markers if a query isn’t valid against the console configuration associated with the project etc.

If you are interested in Java Web Development, see also the article some facts about the history of Java Web Frameworks.