The amazing adventures of Doug Hughes

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.
Productivity
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
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.
Performance
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.
Vendor Independence
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.

Comments on: "Hibernate & ColdFusion 9 – What is ORM?" (7)

  1. Do all ColdFusion 9 applications using the ORM (Hibernate) require xml files representing the relational data at the object level?

    Like

  2. @Mike – In the test application I am playing with right now, there is not XML at all. With the CF9 ORM integration, you simply annotate a CFC object with <cfproperty tags that describe the properties of that object. The arguments added to those property tags define things like data types, relationships, lazy loading, etc.

    So in answer to your question, thus far I have not written any XML for this application and have a fully functional data model. I believe the possibility for some XML configuraiton exists, but I have not dug that far yet.

    Like

  3. That is great news. I wasn’t to excited about keeping tabs on an xml file for database relations.

    Like

  4. Not only do you not have to create the Hibernate mapping xml file (although you can), you don’t even have to include cfproperty tags if you’re mapping an existing table. Hibernate can introspect your database and determine the properties of your object based on the columns in the database.

    Of course this will not allow you to do anything more advanced than define a standalone object. To create relationships you’d have to use cfproperty tags, but the option is open to you.

    Like

  5. Just to put in my own 2c here – I actually prefer doing my ORM mappings for CF9 with XML mapping files.

    I find it gives me far more control over what Hibernate is doing, and I find it a lot easier to format some fairly complicated mappings.

    (As a side note, when working with POJOs I prefere annotations, so go figure).

    I think this is just one of those preference things.

    But if you are using XML mappings, I don’t find they add any extra work to my process, so don’t dismiss them straight away. There are also several things you can only do in XML mappings, although I’m sure that gap will decrease with CF releases.

    Like

  6. Thanks Mark. Some good points to think about.

    Like

  7. Very nices article…

    Like

Comments are closed.

Tag Cloud

%d bloggers like this: