Wed 05 Sep 2007
Just wrapping up a project and thought I would share my experience of how God awful it was working with Hibernate on this project.
This post sums up the bulk of my problems. Hibernate definitely has the high-profile backing of the Java community, is JPA compliant now and Gavin King is pretty much the DHH in the Java Persistence world. Not to mention he’s probably making some good coin at his new home (JBoss). However it is my belief that it only works well for greenfield schemas. That is, create your schema the way Hibernate wants them but if you are thinking of trying to use Hibernate on a legacy schema, that perhaps isn’t well formed you are in for a world of hurt.
iBatis on the other hand is the working man’s ORM. In fact, I think I like it because I (and probably you) have created something similar back in the day before all these formalized ORMs. It simply and elegantly maps your sql results to an object of your choosing. You state the SQL you want to execute, be it a plain-jane SQL statement/stored proc/view and it automatically maps the resultset back into POJOs. It also easily lets you override the default mapping behaviour.
Defining 1:1, 1:M, M:M relationships are far more straightforward than in Hibernate, ESPECIALLY when your database was not designed by you and you have no control over primary keys, foreign keys, etc... (i.e. the BKG schema has no foreign keys.... Hibernate really likes those...)
The solution to the problem in my post (linked above) was to create a view for Hibernate to map to. Unfortunately, I could not update through that view so I had to code custom <sql-update>, <sql-insert> and <sql-delete> tags for my mapping. And believe me, that is literally a bolt on solution Hibernate provides without any frills. You have to actually *run* the hibernate mapping in debug mode to figure out what order hibernate will pass parameter values to your <sql-*> tag. That is extremely fragile! The next person that touches your mapping, perhaps to add or remove a property, will inevitably break the <sql-*> functionality. I absolutely hated using the <sql-*> functionality because of this reason.
What I did at home was I read the iBatis SqlMapper documentation. After that, it took me 30 mins to implement functionality that took me a week to implement in Hibernate. That’s right, a week. A week sounds like a long time, but I had faith that Hibernate would give me a way to map my pojos without resorting to the <sql-*> tags, but it was only an illusion which I realized after reading the responses (or lack there of) to the post I made on the Hibernate forums. So most of the time spent was learning that Hibernate doesn't easily support what I needed it to.
Why is Hibernate more popular? It writes your sql for you, iBatis doesn’t. However, iBatis does have a code generator by the name of Abator (along with an Eclipse plugin) which will do 80% of the grunt work for you by creating your model object, sql mapping files, etc... based off a set of database tables.
So in conclusion, I think we have to be open to using technologies other than what is officially mandated; when it makes sense. I don’t think it’s healthy to say "do we use Hibernate OR iBatis for all projects" because in my case iBatis is definitely something that would have made my life easier, saved the firm money and probably would make the DAs more comfortable knowing that some ORM is not creating Voodoo SQL on-the-fly.
Posted by Massimiliano Dessì on September 05, 2007 at 11:58 AM CDT #
You have a much more serious problem than picking the right ORM tool.
Posted by 85.1.160.122 on September 06, 2007 at 09:39 AM CDT #
Just to give you background on the application, it's a series of CRUD screens which allow the user on our side to fix problems with the data imported from the 3rd party.
Also, welcome to the real world :) I worked for one of the biggest investment banks in the world and they did not use foreign, or even formal primary keys on their data. They used unique indexes and stuff. It drove me mad.
Posted by Craig on September 06, 2007 at 09:56 AM CDT #
But again, for a legacy system with an existing database schema, I agree Hibernate is not a good solution.
Posted by Gregg Bolinger on September 06, 2007 at 09:57 AM CDT #
This article is interesting, but I must give my opinion on this.
I also was confronted to a case where mapping classes towards a poorly designed database system was a nightmare. You always end up doing some special tricks that are very fragile, and I really believe hibernate is NOT suited for a database first <> mapping next, except if the database is well designed and includes good integrity references.
I believe that Hibernate is invaluable when a new project is going to rise. You can very well start by a hbm first design, and generate your database with very fine grain detail. There is very few things hibernate doesn't allow you to do when generating the DDL schema.
You can even generate POJOs with hibernate tools from hbm, so you could use an hbm first application design from the start, and base both your POJOs and your DDL on them, making it for a strong and stable single point entry for both database and persistent POJO creation.
Posted by KiLVaiDeN on September 06, 2007 at 10:11 AM CDT #
Now, given the nature of open source I should update their documents to provide better examples and explanations based on what I have found....
Posted by Craig on September 06, 2007 at 10:26 AM CDT #
Posted by Gregg Bolinger on September 06, 2007 at 10:46 AM CDT #
Posted by suraj chhetry on September 06, 2007 at 11:41 PM CDT #
Posted by Anonymous on November 15, 2007 at 10:27 AM CST #
But I am encouraged to check out iBatis. I arrived on this job with a massive and well-tuned Hibernate setup in place, and got to learn from good examples as I went. I can't imagine someone new to Hibernate building something like our application from the ground up. It works spectacularly well, but no question it's complex.
Posted by Mike Swierczek on November 19, 2007 at 01:58 PM CST #
I have an ORM that is JPA/Hibernate like (but simpler) and I am developing its (IBatis like)... raw SQL to bean functionality.
I'm interested to find out what your requirement was to see how I'd do it... I'm thinking you where selecting from a view and wanted to define insert, update delete dml with named parameters? I wonder if I'm missing something?
You can check out Ebean ORM at www.avaje.org.
Thanks, Rob.
Posted by Rob on December 14, 2007 at 11:28 PM CST #