Use Hibernate narrowly!
Hibernate is a good library. Sure — I first loved it too much, as many of us have. At first, it seems to be the magical solution to the whole persistence layer, probably the most complicated and potentially troubling part of an application.
In the first stage of falling out of love with Hibernate, you discover that there are some aspects of persistence where you need to understand what you’re doing, and Hibernate doesn’t necessarily make it easier. My big awakening was optimistic locking: if you have the same entity in front of two users, how do you avoid the second person’s “save” action overwriting the first? In Hibernate, you add versioning to your entities, so that a new database column contains some unique, incrementing identifier that needs to match the property of the object that’s to be saved. This caused some big headaches with my client at the time, who had a screwy database schema where a created_date and modified_date on each row of each table were not both filled in for a new entry created in the Oracle Forms app, so neither column could be the versioned identifier for Hibernate.
The second stage of falling out of love is where you want to use Hibernate in a non-CRUD scenario – you are not showing individual records to a user for them to be edited. Data import, for example, is one of these things where Hibernate may do things you’d rather it didn’t. Even after disabling the second-level cache, Hibernate was still holding onto some reference to each entity. It wants to manage the objects it persists, and if you’re saving thousands of objects to the database,you’ll probably blow the heap. No doubt there was a fix for this — but even after using a StatelessSession (which helpfully doesn’t implement Session) I still had problems.
The moral is, keep in mind that JDBC, with a good library providing boilerplate templates like Spring-JDBC, is still the right tool for many jobs. I’m often guilty of wanting to apply the new tool (hammer) to all problems (screws). Don’t be that every-problem-looks-like-a-nail guy. It’s immature.
No comments yet.
Leave a comment
About Me
Tweets
- @LaChilangringa thank you, he will be called Walter and might like trains or frogs. You were at the rally? What did your sign say? in reply to LaChilangringa 2010-11-06
- It says I'm not eligible to get a payout in the Buzz settlement. I'll have to settle for juggling with the Buzz developers. :) 2010-11-03
- It's Movember and you can sponsor my mustache. http://goo.gl/Z1O4 I miss the beard; It's very drafty on my face today. 2010-11-02
- Can 4 guys make themselves look enough like Mount Rushmore to fool Google Goggles image search? Love the demo slam. http://demoslam.com 2010-10-20
- Saw Dalai Lama on Thurs, running last 6mi of SF women's marathon with Peggy today. Too many crazy crowds this week! 2010-10-17
- Attn: people of the future. We wanted to avoid all that litter! It was our 2nd priority, right after annoying noises. http://bit.ly/cJzkGT 2010-10-09
- Headed to Hardly Strictly bluegrass in GG park. Elvis Costello free! 2010-10-03
- I vote that @TCooganPlants is having a rough week and deserves nachos. Who's with me? 2010-09-29
- More updates...
Powered by Twitter Tools