Custom Conversion [message #1196535] |
Tue, 19 November 2013 13:13 |
Peter Kullmann Messages: 240 Registered: July 2009 |
Senior Member |
|
|
We are using EclipseLink JPA with an OpenEdge database and we have a problem with a timestamp field which is also used as version:
@Basic()
@Column(name = "mutdatime")
@Version
private Timestamp changedOn = null;
The mutdatime field in the database is defined as DATETIME-TZ which is a timestamp field that includes timezone information. The JDBC driver (by DataDirect) maps the field as a String field and produces timestamps like this:
2013-10-09 11:05:58:528 + 02:00
The default platform of EL works pretty well with these timestamps but we noticed problems as soon as our daylight saving changed in october.
We would like to do the java.sql.Timestamp <--> String conversion ourselves in order to fix issues with the time zone. For this we created a subclass of org.eclipse.persistence.platform.database.DatabasePlatform and have overridden the following methods:
@Override
public Object convertObject(Object sourceObject, Class javaClass)
throws ConversionException {
if (sourceObject instanceof String && javaClass.equals(Timestamp.class)) {
// do our own conversion
}
return super.convertObject(sourceObject, javaClass);
}
@Override
public Object convertToDatabaseType(Object value) {
if (value instanceof Timestamp) {
// do our own conversion
}
return super.convertToDatabaseType(value);
}
@Override
public void setParameterValueInDatabaseCall(Object parameter,
PreparedStatement statement, int index, AbstractSession session)
throws SQLException {
if (parameter instanceof Timestamp) {
String converted = (String) convertToDatabaseType(parameter);
statement.setString(index, converted);
return;
}
super.setParameterValueInDatabaseCall(parameter, statement, index,
session);
}
This works but I'm not sure whether I have gotten the right methods. For instance, overriding setParameterValueInDatabaseCall seems unnecessary because there already is a conversion for this direction in convertToDatabaseType, but it is not used except for the log. Can anyone point me to an example or to documentation for this kind of platform adaption?
Thanks & best regards,
Peter
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 1.06422 seconds