Hibernate Object Relational Mapping
First in a two-part series by Java guru John Hunt
How many Java applications have you built that store data in a database? For me, almost all the Java systems I have been involved with have, at some point, involved a database. In general, what has happened is that data held in objects, at some point has been stored into the database, so that it can be restored back into objects later. Thus, the database has acted as a persistent storage device for information required by the Java system. This can of course be achieved in a variety of ways, and the use of JDBC lies at the heart of this process.
However, mapping the Java (object oriented) world into the relational world of modern databases is not without its own set of issues. The object world and the relational world are two very different paradigms. The Object world deals with classes, instances of those classes and relationships between those instances. The relational world instead deals with tables, rows and the relationships between those tables. In the middle sits SQL, which is a declarative language used to specify a function that determines which columns and rows are returned at any one time.
While this may not be too much of an issue for a single object that can be easily mapped to a single table in the database, things get more complicated in the real world. If what you need to persist is a network of objects, which map to numerous tables (where there may or may not be an object-per-table mapping) this becomes much more complex and the amount of SQL you may need to write can very quickly grow.
Thus mapping between the two paradigms is not trivial and indeed to be successful at this you need to have a reasonable level of expertise in Java, SQL and database design. These skills are not only in demand, but are also hard to come by. An Object Relational Mapping tool (ORM) tries to alleviate this process by providing the means to automatically map form the object world into the relational world (and back again). It aims to make the database storage of objects (and even networks of objects) as simple as Java’s serialization.
Hibernate (available here) is an example of an ORM. It is probably the most widely used open source Object Relational Mapper currently available.
Hibernate is an Open Source Object Relational Mapper. Its aim is to remove the majority of the drudgery and time consuming repetitive coding needed for implementing your own Java-to-Relational mapping system. Thus, Hibernate handles the task of mapping classes to tables, and objects to actual rows. Hibernate generates SQL for you, relieves you from manual result set handling and object conversion and keeps your application portable to all SQL databases (no mean feet in itself).
In particular, Hibernate provides for transparent persistence (at least form the point of view of the object being persisted) - the only requirement for a persistent class is a no-argument constructor. Hibernate is one of the most mature and most complete open source object-relational mapping tools. It is widely used and very actively developed; it is also appearing in more and more systems including JBoss and is often combined with Spring. In the case of JBoss, Hibernate is a critical component of theJBoss Enterprise Middleware System (JEMS) suite of products.