Hi.
I need to make a custom type that
- will set -1 on the object field if the db field is null
- will store null in db if the object field value = -1
I made one. It works for save but an error is returned on get.
I found that it was because of
Code:
public final Object nullSafeGet(ResultSet rs, String name) throws HibernateException, SQLException {
Object value = get(rs, name);
if ( value==null || rs.wasNull() ) {
if (IS_TRACE_ENABLED) LogFactory.getLog( getClass() ).trace("returning null as column: " + name);
return null;
}
else {
....
This method, what ever is returned by get() will return null if the column was null.
So I can't return -1.
And as nullSafeGet is final, I can't override it.
I don't understand why it isn't rely on the get() method and return it's value what ever it is ?
Also, it is not my problem actually, but I saw that I wouldn't be able to store '-1' for a null field because there is another method
Code:
public final void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
if (value==null) {
if (IS_TRACE_ENABLED) LogFactory.getLog( getClass() ).trace("binding null to parameter: " + index);
st.setNull( index, sqlType() );
}
else {
if (IS_TRACE_ENABLED) LogFactory.getLog( getClass() ).trace("binding '" + toString(value) + "' to parameter: " + index);
set(st, value, index);
}
}
that did not call set() of my type if the value is null.
Somebody can see a workaround ?
Thanks.
Mike[/quote]