... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2010-11-25

Hibernate User Types vs. Field Access

In Hibernate (3.2), one can define persistence entity properties that have customized types. There is, however, a bug that arises if the property is mapped with an access type of field. In that circumstance, the following two problems occur if the entity is accessed through a proxy:

  • a reference to the property through a getter method will not trigger initialization of the proxy
  • even if the proxy has been initialized, a getter method will still return an uninitialized value instead of the calculated custom field value (in a debugger, one can see that the proxy is initialized and has a properly loaded target object with the correct custom value -- but the getter returns the uninitialized value just the same)

These problems disappear if the custom-valued property is defined with property access (i.e. getter/setter).

Regular, non-custom properties do not exhibit these problems. I do not know whether this problem has been fixed in later versions of Hibernate. I could find no mention of the problem on the 'net. But then again, Hibernate 3.2 is all but obsolete -- I'm just stuck with it right now.

The second problem may be partly a consequence of another proxy pitfall: proxy field values never change -- even after the proxy has been initialized. The field values will be whatever the no-argument constructor assigns to them (typically zeroes and nulls). You must access proxied properties through this or a getter/setter.

There is a consequence of uninitialized proxy fields that had never occurred to me, but which popped up repeatedly while I was Googling for the original custom-type problem. Uninitialized proxy fields will be particularly apparent in equals() methods (which are virtually impossible to get correct in a Hibernate context anyway, but that is a story for another day). It is extremely common to see code like this._field == that._field in equals() methods. If that is a proxy, this code will not work since the wrong field value will be obtained. Advance Hibernate Proxy Pitfalls on the Xebia blog is one of many examples of discussion on this topic.

2010-11-11

Google Refine

Freebase Gridworks is now called Google Refine, a consequence of Google's acquisition of Metaweb.

Blog Archive