Hibernate tips and tricks
I was prompted to write this post to mention a "featue" that I discovered in hibernate. I've been using hibernate for a little while and have found it to be a love / hate relationship. It may be that I just don't have enough experience with it to fully appreciate the productivity gains that seem to dangle so tantilisingly close. Whatever it is, I'm too far down the track to turn back, so I labour on in the hopes that one day it will all make sense.
When using a <timestamp /> in my bean mapping and in conjuction with using session.merge, the new tmestamp value is not updated in the original object after it's persisted in the database. i.e. hibernate updates the timestamp in the database, but it's not reflected in the original class.This behavior differs from the behavior of session.update, where the new value IS correctly reflected in the original object.
The reason for using merge is to update a detached object, but one would think that in doing the merge the object would be re-attached to the session and synconised with the database version. This is what the doc imply in any case.
The issue I have with hibernate is that there are some less than transparent behaviours being exibited. Even if this behavior was documented, it is less than intutive and seems like an oversite on the part of the developers.
Another example of this kind of 'side-effect' based behaviour is the very reason I'm using merge to start with. Cascading deletes won't work on detached objects, so hibernate requires you to use merge in order to re-attach the object to the session. In order to do this it first needs to read the object in from the db before doing the update(merge). This kind of makes sense in that it would be difficult for hibernate know which related sub-classes in the object graph would need to be deleted, but for performance reasons it's a bad idea in my opinion. It would be possible use the intersection of the parent and it's composite classes to cascade the deletes. i.e. delete from composite_class where parent_id = ? and composite_id not in (?,?,?).
Anyhow, there are allot of benfits to using hibernate, but annoying little tricks like that have been slowing down deveopment for us in a big way.
Update: Here are a few links from people talking about the issue with cascading deletes on detached objects.
- http://hapmoorii.blogspot.com/2006/01/will-i-use-hibernate-again.html
- http://forum.hibernate.org/viewtopic.php?p=2376122
- http://forum.hibernate.org/viewtopic.php?t=928459
Update 2: I posted the issues on the hibernate forum. Let's see if anyone has any reasonable solutions. http://forum.hibernate.org/viewtopic.php?t=986706

There are no comments for this entry.
[Add Comment]