Search code examples
hibernategrailsgrails-ormhibernate-mapping

Stripping trailing whitespace from char fields in a legacy database with Grails GORM


What are the possible solutions for stripping the trailing whitespace when mapping char fields in a legacy database?

I see the following options:

  • Calling .trim() at the point of use (controller, view, etc)
  • Override property accessors to return .trim()
  • Using a Hibernate UserType to trim the whitespace

I'm leaning toward overriding the property accessor so that the domain properties remain consistent throughout the application.


Solution

  • I had a similar problem and I could not alter the legacy data. I ended up overriding the accessor for the sake of transparency to my fellow developers. I would recommend using a transient field so you don't have to trim the String on every call. However; if you can just clean up the data in the table, create a view, or transfer to a new table I would do that instead.