Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 789
James Allman Avatar asked Jul 12 '11 17:07

James Allman


1 Answers

I use a globally mapped Hibernate UserType and it works great (implementation based on http://www.hibernate.org/388.html, but updated for breaking changes to the UserType interface):

package company

import org.hibernate.Hibernate
import org.hibernate.usertype.UserType

import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Types

/**
 * Map CHAR(x) types to String: trim when getting and setting the CHAR(x)
 * based on www.hibernate.org/388.html
 */
public class TrimmedString implements UserType {
    public TrimmedString() {
    }

    public int[] sqlTypes() {
        return [Types.CHAR] as int[];
    }

    @SuppressWarnings("unchecked")
    public Class returnedClass() {
        return String.class;
    }

    public boolean equals(Object x, Object y) {
        return (x == y) || (x != null && y != null && (x.equals(y)));
    }

    public Object nullSafeGet(ResultSet inResultSet, String[] names, Object o) throws SQLException {
        String val = (String) Hibernate.STRING.nullSafeGet(inResultSet, names[0]);
        return val == null ? null : val.trim();
    }

    public void nullSafeSet(PreparedStatement inPreparedStatement, Object o, int i) throws SQLException {
        String val = (String) o;
        inPreparedStatement.setString(i, val);
    }

    public Object deepCopy(Object o) {
        if (o == null) {
            return null;
        }
        return new String(((String) o));
    }

    public boolean isMutable() {
        return false;
    }

    public Object assemble(Serializable cached, Object owner) {
        return cached;
    }

    public Serializable disassemble(Object value) {
        return (Serializable) value;
    }

    public Object replace(Object original, Object target, Object owner) {
        return original;
    }

    public int hashCode(Object x) {
        return x.hashCode();
    }
}

global mapping in Groovy.config:

grails.gorm.default.mapping = {
    'user-type'(type: company.TrimmedString, class: String) //map Char(x) columns mapped to string fields as trimmed string
}
like image 177
Stephen Swensen Avatar answered Sep 23 '22 06:09

Stephen Swensen