We're working with a really old database where dates are stored as numeric values in the database as YYYYMMDD
, for example, for today (July 16th 2014) it would be stored as 20140716
.
We're currently solving this by implementing our own AttributeConverter<Date, Long>
which converts Date
objects to Long
and vice versa.
However, there's also a constraint on the database that does not allow NULL
values in the column. The column itself is optional, but if the date is not existing, the date should be persisted as zero (0
) in stead of NULL
.
So, what we did in our AttributeConverter
is the following:
@Override
public Long convertToDatabaseColumn(Date input) {
Long out = 0L;
if (input != null) {
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(input);
out += (cal.get(Calendar.YEAR) * 10000);
out += (cal.get(Calendar.MONTH) + 1) * 100;
out += cal.get(Calendar.DAY_OF_MONTH);
}
return out;
}
But while debugging we noticed that our converter is not executed when the date is null
, so it's never converted to zero.
Is there a way we can still force Hibernate (4.3.5.Final) to use our converter, even if the value is null
?