3 Mar 2009

I was stuck a while ago trying to figure out how to map PersistentDateTimeTZ in a GORM class. It's an implementation of Hibernate's UserType interface that persists a Joda DateTime value using 2 columns - one for the actual timestamp and one for the time zone. The DateTime class contains time zone information but a SQL timestamp column is time zone agnostic so you can lose data when the value is saved (the same exact problem exists when persisting java.util.Date values).

I could never figure out how to map the user type to my domain class property correctly. Just doing:

static mapping = { dateTimeProperty type: PersistentDateTimeTZ}

Failed with:

org.hibernate.MappingException: property mapping has wrong number of columns.

I finally found the solution in the 2nd Edition of The Definitive Guide to Grails (which I can't recommend enough, by the way). The trick is to pass a closure specifying the two columns to the property definition in the mapping builder. The working code looks like this:

Hmm. I'll have to experiment with that, however it's very possible that it's not something the plugin can do anything about - it might be a problem with *any* Hibernate UserType that persists to multiple columns. I'll post something here once I figure anything out.