Let’s put Hibernate to sleep ?

Don’t get me wrong, Hibernate is a great tool written by very smart people, but I keep wondering how this technology has become so popular.

Maybe it’s just me, but for the past 3 years now I have not come across one single developer that grasps half of the concepts needed to understand how Hibernate works. They use it and don’t understand it.

This is a trend I have been witnessing more and more. Developers pick a technology because it is the hype of the moment, start to use it, get a hard time to get it to do what they want, but keep using it and advocate it. Probably because they don’t want to admit to the world they don’t understand it.

This is in my opinion what makes Hibernate (and the like) so popular.

To illustrate my point, we were just contracted by one of our customers to take over the maintenance of a codebase written by another company.

I will make no comment on the code itself, since I tend not to judge code written by others since code is always written in a specific “context” and, in my opinion, can not be judged without knowing that context.

The main complaint of the customer was that the application was getting slow. He had to wait from 5 minutes to 15 minutes to get one specific screen to dispay (I am not exagerating).

So we started  a code analysis and had the chance to witness exactly what I was mentioning above. All the code was written exactly as if practice existed in “theory”.

The application had a magnificent (ironic) data model with 50 tables (entirely generated by Hibernate). An object graph sometimes 5 or 6 level deep.

While looking at it, I could hear the developer say on day 1 : Wouaw, this is so cool … I can do getEmployee().getDepartment().getRegion().getXXXX().getYYY().add(new ZZZ()) .. and it all works ..

And then the nightmare begins : lazy loading, hibernate session (open session in view issues), object identity, HQL, slow queries, etc ..

And so the developer (which never abandons) tries to “understand” by finding workarounds, tricks, breaks his architecture and does everything that is needed to make this nightmare work.

And so in day 15 (or 200 depending on the company you work for) : the code works (or seems to work) and nobody can touch it anymore because nobody understands it (except of course the main developer).

In our case, in production with real data (1.000.000 records or so – which is not even that big) the application is “curiously” verrrrrry slow ..

Just turning on the “show sql” in hibernate generated 3 Mb logs on every click …

The conclusion here is that, in my opinion, it is not a good idea to try to “hide” your database behind a “magic” object model. Even if Hibernate does its best to achieve just that, I think the challenge is not worth it .. The database will always pop back on you .. So embrace it ..

So we decided to remove the Hibernate layer and replaced it with IBatis. Hopefully the code was well structured and we were able to replace the Hibernate DAO layer by and equivalent (somewhat adapted) IBatis layer but not touching the above service layers.

When we choose a technology, we usually expect the following :

  • It should pass the 10 minute test
  • The learning curve should not be too steep
  • It should serve a specific purpose and serve it well
  • The technology should be understandable by a junior developer after 30 min introduction

Pierre

3 Responses - Leave yours here.

  • Either we have been working with the same people, either the situation is even more desperate…

    We are working with Hibernate since versions 1.x and are very happy with it. We came to it because we quickly realized that EJB persistent entities wasn’t an option for us (at that time and in our context) and found Hibernate as a potential candidate. It wasn’t a “standard” yet so we had to convince people… You know what I mean: everybody is talking about EJB as *the* solution and you come and say it “sucks”, lets go for something else. Anyway, it was a wise decision and never had to regret it.

    Oh well, it wasn’t fun all the time. The product was still very young and we hit some bugs and other sorts of shortcomings… We even had to dig into the code and provide fixes to the community. It was very funny and exciting. Today the application is still in production (5 or 6 years) and customer is still very happy with it.

    As a result, we got a very good understanding on how Hibernate works, what is the rational behind it and what it is good for or not. It always makes me sad when I see people using it without actually understanding the product. How many times have I see someone calling save() every time she makes a single change to a persisted entity… How many times the application is performing badly because of the n+1 fetch problem, or the lazy loading issues, etc. And even transactions – most people don’t see the need for a proper transaction management!

    It sometimes makes me cry: Oh God, is it possible so many people don’t even understand what they are doing ? How comes those people pretend they know the technology? It is enough to successfully persist a single entity to master the entire framework? Even worst: most of the time they are not even *aware* of the problems – and ultimately, if it doesn’t work or perform nicely, they blame the technology for not being good enough. Why don’t they first question themselves? The product is used by millions of people on thousands of different projects and strange enough, people declare it is not suitable for their own particular case. Are they sure they understand how to use it? They probably don’t even ask the question… :(

    I remember someone who was having problems with a shell script he wrote in bash. Bash… you know, that thing exists since ages and is in place in nearly every unix operating systems. After 2 hours looking for the problem, he decided it was a bug in the bash interpreter itself! Well, the bug was actually on the other side of the keyboard…

    A wise guy once said: “le 21ème siècle est le siècle de l’amateurisme”… Think about it – it has never been more true!

  • [...]
    The conclusion here is that, in my opinion, it is not a good idea to try to “hide” your database behind a “magic” object model. Even if Hibernate does its best to achieve just that, I think the challenge is not worth it .. The database will always pop back on you .. So embrace it ..
    [...]

    Indeed – if you think a tool like Hibernate will completely hide your database – you are on the wrong track.
    But still, Hibernate does a good job at helping us to manage our persistence needs and even though the database pops up back at some places, there are clean ways to handle the issues – provided you know how the tool is working.

    Remember: a fool with a tool is still a fool…

  • [...]
    provided you know how the tool is working.
    [...]

    That is exactly my point, since the chance of finding a developer who knows how hibernate is working is rather low, i’d rather not use it, since this might be a future risk of the project. I know this can be theoratically argued, but in practice this is a factor you have to take into account. This of course applies to hibernate and can not be generalized.

Leave a comment

Your email will not be published. Required fields are marked *.