Jason Sheedy's blog

Discussing software engineering with Java, Flex, ColdFusion, Linux, etc, etc .... Life, The Universe and Everything ;)

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.

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

J2EE / Flex developer position avaliable

We currently have a position available at CVC for a full time developer. More info on the position can be found here.

Learning J2EE programming

For the past few days I've been getting my head around J2EE programming. In particular servlet programming, JSP and the Spring framework. Let me just say it has been no simple task and a huge shift from cold fusion programming, but I feel as though I have a pretty good understanding of it now. The reason I'm sharing this is for future reference and also to say what a pleasure it is to work with CF. CF makes development much simpler and quicker. However, the flexibily and control delivered by J2EE is very alluring.

Our company is building a large Flex/AIR based CRM system. We're planning on distributing it to allot of servers and the cost of CF is really prohibitively expensive for the scale of deployment we're planning.

Here are a few of the resources I've been using on my quest. If anyone can recommend any other good resources I would love to know about it. ;)

http://tomcat.apache.org/

http://www.springframework.org/

Introduction to the Spring Framework

Spring MVC step by step