5

In our application we are pulling data from a DB2 mainframe database. If the database has "low values" in a field, hibernate sends a "null" value in the object. This occurs even if the column is defined as "not null".

As we are doing XML parsing on this, Castor is having trouble with it. I would like to fix this in Hibernate. Also, all of the hibernate hbm files are generated, so we can't mess with them (they are regened from time to time.)

Any way to intercept all Strings and replace nulls with ""?

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
markthegrea
  • 3,731
  • 7
  • 55
  • 78

4 Answers4

3
  • Create a custom user type to replace null with ""
  • Put the user type name where you normally put a hibernate type name in reveng.xml.
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • This was our first thought and the one we will probably implement so far. I was hoping for a Dialect or configuration change, but this might be the best we can get. Thanks! – markthegrea Apr 15 '10 at 14:52
  • @sliver I'm not aware of something like that at the dialect level (this tweak seems too specific to be in the dialect) but I may be wrong. My feeling is that a UserType is the way to implement this. – Pascal Thivent Apr 15 '10 at 15:50
1

Here is the Solution we (credits to @Betlista and @markthegrea) came up with. Actually, this is an upper case one and is truncated (lots of abstract methods are removed).

 public class UpperCaseUserType implements org.hibernate.usertype.UserType{
    private static final int[] TYPES = {Types.VARCHAR};

    public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
        //This might be redundant to uppercase the getter...
        return StringUtils.upperCase((String) Hibernate.STRING.nullSafeGet(resultSet, strings[0]));
    }
    public void nullSafeSet(PreparedStatement preparedStatement, Object object, int i) throws HibernateException, SQLException {
        String string = StringUtils.upperCase((String) object);
        Hibernate.STRING.nullSafeSet(preparedStatement, string, i);
    }
}

And then you hook it up this way:

 <property name="partNumber" type="cat.dds.fpsdma.model.UpperCaseUserType">
        <column name="PART_NUMBER" length="20" not-null="true" />
 </property>
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
0

You can use hibernate interceptor which extends EmptyInterceptor to perform the operation you want to before actually firing that query.

the example given here might help you

http://www.mkyong.com/hibernate/hibernate-interceptor-example-audit-log/

GuruKulki
  • 25,776
  • 50
  • 140
  • 201
0

I solve same issue on my side by handling database NULL values in the connector setters, like advised here, with a simple

/**
 * Set the value related to the column: NOM
 * @param nom the NOM value
 */
public void setNom (String nom) {
    this.nom = (nom!= null) ? nom : "" ;
}
J. Chomel
  • 8,193
  • 15
  • 41
  • 69