【发布时间】:2017-09-14 23:16:02
【问题描述】:
我制作了一个转换器,当我从数据库中读取 DATE 字段时,它应该能够将它们强制转换为 java.time.LocalDate 对象。但是,当我尝试这样做时,它给了我这个错误:
The object [3/16/17 12:00 AM], of class [class java.sql.Timestamp], from mapping [org.eclipse.persistence.mappings.DirectToFieldMapping[startDate-->TEST_TABLE.START_DATE]] with descriptor [RelationalDescriptor(com.test.TestEntity --> [DatabaseTable(TEST_TABLE)])], could not be converted to [class [B].
TEST_TABLE 是我的表,其中有一列START_DATE,其类型为DATE。这是转换器:
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Date;
import java.time.LocalDate;
@Converter(autoApply = true)
public class OracleLocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate attribute) {
return (attribute != null ? Date.valueOf(attribute) : null);
}
@Override
public LocalDate convertToEntityAttribute(Date dbData) {
return (dbData != null ? dbData.toLocalDate() : null);
}
}
为什么它认为我的列是时间戳? oracle 中的所有日期都被强制转换为java.sql.Timestamp 吗?
【问题讨论】:
标签: java oracle jpa glassfish eclipselink