As Java developers, we always enjoy working with objects. We thoroughly know how to manipulate, massage, or manage objects in a Java application. However, when it comes to persisting these objects to a relational database, we get slightly uneasy. There is a reason for this—you cannot persist the objects without having to format them in a shape that the database can understand and accept. The most common way is to use some SQL in your code and request the JDBC driver to do the job of persistence on our behalf.
How easy would it be if I could save the object by simply calling a save method instead of writing JDBC code? Likewise, if we wanted to delete a row, is it not convenient to just call upon delete for an object?
We can certainly do this by employing Object Relational Mapping (ORM) tools. These tools’ main job is to transform the Java objects to relational data and vice versa. There are quite a few ORMs available. One that has gained a serious following is Hibernate. Hibernate is an ORM framework that lets you work on Java objects without having to worry about SQL queries, database connections, datasources, and resource management issues. While certainly you can use Hibernate on its own in an application, using it via Spring may bring more advantages such as dependency injection and testability. This chapter looks into Spring’s Hibernate offering in the ORM space while future chapters will discuss others such as iBatis.
Over the years, Hibernate ...