我最近尝试将我的应用程序从 JBoss EAP 7.0 部署到 7.2.4,从那时起,我就遇到了带有 的字段的架构验证问题LocalDate
映射到 date
类型的列。
确切的失败消息是:
{"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"project-name.ear#MyPersistenceUnit\"" => "javax.persistence.PersistenceException: [PersistenceUnit: MyPersistenceUnit] Unable to build Hibernate SessionFactory
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: MyPersistenceUnit] Unable to build Hibernate SessionFactory
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [someDate] in table [SOME_TABLE]; found [date (Types#DATE)], but expecting [timestamp (Types#TIMESTAMP)]"}}
架构中的片段:
create table SOME_TABLE(
-- primiary key and other columns
someDate date
);
实体中的字段:
@Convert(converter = LocalDateConverter.class)
private LocalDate someDate;
转换器
import java.sql.Date;
import java.time.LocalDate;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class LocalDateConverter
implements
AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate attribute) {
return attribute == null ? null : Date.valueOf(attribute);
}
@Override
public LocalDate convertToEntityAttribute(Date dbData) {
return dbData == null ? null : dbData.toLocalDate();
}
}
我发现按照建议添加 @Column(columnDefinition = "date")
here解决了问题,但我的问题是:
- 在这种情况下,我是否必须强制添加
columnDefinition
? - 该应用程序在 JBoss 7.0 中运行良好。我如何知道 JBoss 7.2.4 中引入的哪些更改破坏了我的应用程序?
- 解决此问题的任何其他建议。
这是 H2 的问题,而连接 Oracle 则没有此问题。
最佳答案
已更新
在 Oracle 模式下使用 DATE 时,您似乎遇到了 Hibernate 与 H2 驱动程序不兼容的问题。请参阅:https://github.com/h2database/h2database/issues/1824
此外,JBoss 7.2 现在支持 JPA 2.2(请参阅:https://access.redhat.com/articles/113373)。在 JPA 2.2 中添加了对 Java8 时间类(如 LocalDate)的支持,因此您可以删除转换器。
JPA 2.2 使用 JDBC 4.2。以下是 JDBC 中的对象映射。
请参阅 JDBC 4.2 spec 上的表 B-4 :
关于java - LocalDate 的 hibernate 模式验证失败 - 找到日期,但需要时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60725695/