I am trying to integrate to a legacy ingres database system.
Unfortunately, this system relies on the fact that an empty string can be successfully stored in a date field, to indicate that a date has not been stored (as opposed to making it a nullable field).
I can successfully read the date field and get an empty string returned using standard JDBC code.
Unfortunately, when reading via Hibernate, an empty string becomes "1970-01-01 00:00:00.0" ...... obviously the default date.
I have looked at tyring to implement a UserType to handle this, along the lines of the previous example (as I can happily use the date as a string in the Java class).
I have tried using :
Code:
public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
}
and
Code:
public int[] sqlTypes() {
return new int[] { Types.DATE };
}
but an empty string is still being returned from the database as ""1970-01-01 00:00:00.0".
The full code listing is :
Code:
public class TrimmedDateString implements UserType {
public TrimmedDateString() {
super();
}
public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
}
public Class returnedClass() {
return String.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
return (x == y) || (x != null && y != null && (x.equals(y)));
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
String val = rs.getString(names[0]);
System.out.println("nullSafeGet: val is " + val);
if (null == val)
return(null);
return val.trim();
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
st.setString(index, (String)value);
}
public Object deepCopy(Object value) throws HibernateException {
System.out.println("deepCopy: value is " + (String)value);
if (value == null) return null;
return new String((String)value);
}
public boolean isMutable() {
return false;
}
}
I could, test if the returned string is "1970-01-01 00:00:00.0" and then replace it with an empty string, but is there any other alternative ?????
I have not yet tried storing an empty string.
Any suggestions (I am stuck with the way in which the legacy database has been implemented, as that application is still successfully running and will continue to do so for a long while).
Thanks,
Mike.