PERSISTENCE AND HIBERNATE
Understanding the Complexities of Persistence
One of the most complicated and time-consuming tasks of developing an enterprise
application is writing the code to store and load data from a database at
the appropriate times.
At some point, the state of your application must be persisted — most likely to a relational database. Following are some obstacles to overcome with this:
· Mapping an object model to a relational schema — The types of relationships exhibited differ between the two types of models. For example, relational databases model one-to-many and many-to-many relations; whereas, object models model association, aggregation, composition, multiplicity, and inheritance.
· Keeping your object model and database schema in sync — Why should you have to? If you define a Person class with all its fields, you shouldn’t have to duplicate your effort by defining the same structure in a table. What happens when you change the class but you forget to change the schema?
· Persisting and retrieving an object from a database — Writing SQL for all the fields is very tedious and error-prone. It is another thing that also has to be updated when the model changes.
· Maintaining relationships — When an object is retrieved that is associated with a second object, where should the code go that retrieves this second object? When should it be called?
· Querying data — Where do you write code to find all Person objects who are born in a particular year?
· Performance — Executing an SQL hit for every single operation is slow and puts a lot of overhead on the server. Techniques such as caching, lazy loading, and eager loading must be employed to improve performance.
The actual work required to achieve all this is possible but very repetitive, which adds to development time. However, like all repetitive tasks, tools can be of assistance.
Hibernate is one of those.
Persisting Objects with Hibernate 2
Hibernate version 2, which will be referred to from here on as Hibernate, is a free and Open Source Object-Relational Mapping (ORM) framework available from http://hibernate.sourceforge.net.
Hibernate is not an abstract, high-level persistence framework like JDO or EJB
CMP. Unlike those frameworks, it doesn’t try to abstract away completely to a
generic persistence framework that is applicable to mapping to relational, objectbased,
or mainframe databases. It specializes only in transparent persistence of
objects in relational databases and, thereby, has an API that is more intuitive
for working with relational databases. Also note that, unlike those standards,
Hibernate is not currently an implementation of any industry standard. Hibernate tries to accomplish what is known as Transparent Persistence in its best and most realistic form. Transparent Persistence means that, in order to add the persistence functionality to classes, you don’t have to modify the classes to adapt to the persistence mechanism. Some frameworks, such as JDO, use byte-code modification to accomplish this. So, in those systems, the compiled classes are enhanced by some persistence-specific bytecode. Hibernate, on the other hand, does essentially the same thing but in runtime.
The advantage of this approach is smoother development cycles because no extra step is added to the build process of the application to enhance the compiled classes
with persistence code.
Hibernate works with many different database brands, such as Oracle, DB2,
Sybase, PostgreSQL, and MySQL, to name a few. It understands different SQL
dialects of these databases and tries to take advantage of all optimizations possible
in the SQL dialect of each of these databases. It’s worth mentioning that
Hibernate is independent of any application server and works on any of them. It even works in environments other than J2EE application servers. For example, you can easily use it in a client-server Swing application or in JUnit test cases.
Hibernate comes with a framework for storing objects in relational tables. It provides an API to load those objects back in memory, either directly by using the primary keys or by using a sophisticated query language called Hibernate Query Language (HQL). It provides other services such as caching and database schema generation, too.
One of the most complicated and time-consuming tasks of developing an enterprise
application is writing the code to store and load data from a database at
the appropriate times.
At some point, the state of your application must be persisted — most likely to a relational database. Following are some obstacles to overcome with this:
· Mapping an object model to a relational schema — The types of relationships exhibited differ between the two types of models. For example, relational databases model one-to-many and many-to-many relations; whereas, object models model association, aggregation, composition, multiplicity, and inheritance.
· Keeping your object model and database schema in sync — Why should you have to? If you define a Person class with all its fields, you shouldn’t have to duplicate your effort by defining the same structure in a table. What happens when you change the class but you forget to change the schema?
· Persisting and retrieving an object from a database — Writing SQL for all the fields is very tedious and error-prone. It is another thing that also has to be updated when the model changes.
· Maintaining relationships — When an object is retrieved that is associated with a second object, where should the code go that retrieves this second object? When should it be called?
· Querying data — Where do you write code to find all Person objects who are born in a particular year?
· Performance — Executing an SQL hit for every single operation is slow and puts a lot of overhead on the server. Techniques such as caching, lazy loading, and eager loading must be employed to improve performance.
The actual work required to achieve all this is possible but very repetitive, which adds to development time. However, like all repetitive tasks, tools can be of assistance.
Hibernate is one of those.
Persisting Objects with Hibernate 2
Hibernate version 2, which will be referred to from here on as Hibernate, is a free and Open Source Object-Relational Mapping (ORM) framework available from http://hibernate.sourceforge.net.
Hibernate is not an abstract, high-level persistence framework like JDO or EJB
CMP. Unlike those frameworks, it doesn’t try to abstract away completely to a
generic persistence framework that is applicable to mapping to relational, objectbased,
or mainframe databases. It specializes only in transparent persistence of
objects in relational databases and, thereby, has an API that is more intuitive
for working with relational databases. Also note that, unlike those standards,
Hibernate is not currently an implementation of any industry standard. Hibernate tries to accomplish what is known as Transparent Persistence in its best and most realistic form. Transparent Persistence means that, in order to add the persistence functionality to classes, you don’t have to modify the classes to adapt to the persistence mechanism. Some frameworks, such as JDO, use byte-code modification to accomplish this. So, in those systems, the compiled classes are enhanced by some persistence-specific bytecode. Hibernate, on the other hand, does essentially the same thing but in runtime.
The advantage of this approach is smoother development cycles because no extra step is added to the build process of the application to enhance the compiled classes
with persistence code.
Hibernate works with many different database brands, such as Oracle, DB2,
Sybase, PostgreSQL, and MySQL, to name a few. It understands different SQL
dialects of these databases and tries to take advantage of all optimizations possible
in the SQL dialect of each of these databases. It’s worth mentioning that
Hibernate is independent of any application server and works on any of them. It even works in environments other than J2EE application servers. For example, you can easily use it in a client-server Swing application or in JUnit test cases.
Hibernate comes with a framework for storing objects in relational tables. It provides an API to load those objects back in memory, either directly by using the primary keys or by using a sophisticated query language called Hibernate Query Language (HQL). It provides other services such as caching and database schema generation, too.
<< Home