Convert .NET DateTimeOffset <--> Jodatime DateTime (utc) [message #1053013] |
Wed, 01 May 2013 23:03 |
Michael Gardner Messages: 4 Registered: April 2013 |
Junior Member |
|
|
Hello,
I am creating a RESTful service with JAX-RS, JPA2, and Jodatime. We are using an MS SQL Server database. There is a database table that contains a .NET DateTimeOffset column for an expiration date. It contains the date and time value expressed in the UTC timezone (zero offset). I have an entity that represents this table. When I GET a list from this table, I am receiving the following exception:
javax.servlet.ServletException: java.lang.ClassCastException: microsoft.sql.DateTimeOffset cannot be cast to java.sql.Timestamp
I have found information about creating a converter, but it does not seem to resolve this issue. It converts between Timestamp and Jodatime DateTime values. I cannot find any information about how to convert between DateTimeOffset and Timestamp or Jodatime DateTime types. I would appreciate your suggestions so I can move forward on our project.
Thank you for your time,
Mike
// Assigning the converter to the Jodatime DateTime variable.
@Column(name = "Expiration")
@Converter(name = "dateTimeConverter",
converterClass = JodaDateTimeConverter.class)
@Convert("dateTimeConverter")
private DateTime expiration;
// Converter code from the web...
import java.sql.Timestamp;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.mappings.foundation.AbstractDirectMapping;
import org.eclipse.persistence.sessions.Session;
import org.joda.time.DateTime;
/**
*
* @author Dev
*/
public class JodaDateTimeConverter implements Converter {
private static final long serialVersionUID = 1L;
@Override
public Object convertDataValueToObjectValue(Object dataValue, Session session) {
return dataValue == null ? null : new DateTime((Timestamp) dataValue);
}
@Override
public Object convertObjectValueToDataValue(Object objectValue, Session session) {
return objectValue == null ? null : new Timestamp(((DateTime) objectValue).getMillis());
}
@Override
public void initialize(DatabaseMapping mapping, Session session) {
((AbstractDirectMapping) mapping)
.setFieldClassification(java.sql.Timestamp.class);
}
@Override
public boolean isMutable() {
return false;
}
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03007 seconds