Before we dig into Hibernate, I wanted to take a quick textbook look at what ORM is since Hibernate is an example of an ORM framework. ORM spelled out is object/relational mapping. One text I read said that the slash between object and relational is supposed to emphasize the mismatch problem that occurs when the object oriented world meets the relational world.
The way the book Java Persistence with Hibernate defines ORM is:
.. the automated (and transparent) persistence of objects in a Java application to the tables in a relational database, using metadata that describes the mapping between the objects and the database.
This sounds pretty simple so basically an ORM knows enough about the object model in your application to know how to take a Person object and its related Address etc. instances and transform that data into your relational database structure and vice versa.
Again, according to Java Persistence with Hibernate, an ORM solution is made up of the following 4 components:
- An API for performing basic CRUD expressions think SQL
- A language or API for creating queries that refer to object and their properties instead of tables and column
- Capabilities for describing mappings between objects
- Capabilities for functionality including dirty checking, lazy fetching, and other optimization
There were a lot of buzz words in that last point, but the optimization reference is one I am interested in. ORM solutions in the past have always seemed to just add an extra layer of overhead to an application and thus suffer a performance penalty. If through Hibernate we can optimize the queries that are run and the way data is returned, that could make a big difference. On the flip side, there are quite a few benefits to a properly implemented ORM solution like Hibernate.
As much as the DBAs out there love to write and optimize SQL queries, like any other coding, it still takes time to write and time to debug. In addition, you may have to deal with cross platform issues in dealing with different relational database systems. An ORM framework such as Hibernate can drop right into place and with a few tweaks to the object model, handle all of this persistence layer for us. I expect there will be some optimization work and a few queries that still need to be written, but for the most part, all of that redundant CRUD work is taken care of.
Maintenance goes along with productivity fewer lines of code to be written in the first place means fewer lines of code to maintain down the road. In the case of Hibernate, you are also dealing with an open source, battle tested application rather than starting from scratch with your own persistence layer thus hopefully reducing the bugs you have to deal with.
This one is where I have the most questions. With hand coded SQL queries, you can always tweak the query to get just the data you need. You can cache those query results and more to get every little bit of performance you can. With an ORM solution, it is argued in the book that via Hibernate, these optimizations are even easier to achieve and faster to implement. There is also the argument that with an open source project like Hibernate, there were many more minds looking at the code and had more time to investigate optimizations than you have on any given project. This argument I can see and agree with.
This benefit can be argued as with most custom applications I have ever worked on, you very rarely see a database platform change. However, if you were developing software that was sold commercially and had to support multiple database platforms, I could see this being a big benefit. In order to support multiple platforms without an ORM, you would either be limited to very generic SQL or you would have built multiple persistence layer implementations which would just add to the maintenance overhead. With a vendor independent solution such as Hibernate, this dependency is taken care of for you.
That is enough for today. Next up, the requisite Hello World project what book would be complete without one? The book obviously sets this up in Java, but my plan is to duplicate it on ColdFusion 9 using the new Hibernate integration.