Can Hibernate assign Null String value to empty string

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

Since we are doing XML parsing on this, Castor is having problems with it. I would like to fix this in Hibernate. Also, all hibm hibernate files are generated, so we cannot communicate with them (they are updated from time to time).

Any way to intercept all lines and replace zeros with ""?

Here is the solution we came across. In fact, it is uppercase and truncated (many abstract methods have been 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 connect it like this:

 <property name="partNumber" type="cat.dds.fpsdma.model.UpperCaseUserType">
        <column name="PART_NUMBER" length="20" not-null="true" />
 </property>

      

+2


a source to share


2 answers


  • Create a custom type to replace null

    with""

  • Put the user type name where you would normally put the hibernate type name in reveng.xml

    .


+2


a source


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

the example given here might help you



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

0


a source







All Articles