问题描述
我使用Spring boot 1.4.2,它带来了hibernate 5.0.11(JPA 2.1)。
我想在我的实体中使用Java 8时间类,并且包含 hibernate-java8
。
@Entity
@Table(name =my_alarms_timeline,indexes =>我的实体定义了一个LocalDate字段。 {...})
public class MyAlarm {
...
@Column(name =validity_date)
@NotNull
Private LocalDate validityDate;
}
我希望这可以映射到我的H2数据库中的DATE。 p>
在我的数据库中,我声明这是 validity_date DATE NOT NULL,
。
当我尝试运行测试时,出现以下错误:
[INFO]造成者:org.hibernate.tool.schema.spi.SchemaManagementException:模式验证:错误表[my_alarms_timeline]中的[validity_date]列中遇到的列类型;找到[date(Types#DATE)],但期待[timestamp(Types#TIMESTAMP)]
令我吃惊的是,如果我改变DB定义为 validity_date TIMESTAMP NOT NULL,
我得到错误
由org .hibernate.tool.schema.spi.SchemaManagementException:模式验证:在表[my_alarms_timeline]中的列[validity_date]中遇到错误的列类型;找到[timestamp(Types#TIMESTAMP)],但期待[date(Types#DATE)]
这只是上一个。
我也尝试过使用 hibernate-java8 > AttributeConverter< LocalDate,java.sql.Date> 但这会产生相同的错误结果。
我该做什么,如此我的LocalDate已正确映射到数据库中的DATE?
我也尝试过使用 LocalDateTime
字段映射到一个TIMESTAMP,这工作没有问题...
所以我得到它运行,通过添加 columnDefinition =DATE
添加到 @Column
注释中。
@Entity
@Table(name =my_alarms_timeline,indexes = {...})
public class MyAlarm {
...
@Column(name =validity_date,columnDefinition =DATE)
@NotNull
private LocalDate val idityDate;
}
不知道为什么没有人看到这个问题...
I use Spring boot 1.4.2, which brings hibernate 5.0.11 (JPA 2.1).I want to use the Java 8 time classes in my entities, and so included hibernate-java8
.
My entity defines a LocalDate field.
@Entity
@Table(name = "my_alarms_timeline", indexes = {...})
public class MyAlarm {
...
@Column(name = "validity_date")
@NotNull
private LocalDate validityDate;
}
I expect this to be mapped to a DATE in my H2 database.
In my DB I declare this as validity_date DATE NOT NULL,
.
When I try to run tests, I get the following error:[INFO] Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [validity_date] in table [my_alarms_timeline]; found [date (Types#DATE)], but expecting [timestamp (Types#TIMESTAMP)]
To my surprise, if I change the DB definition to validity_date TIMESTAMP NOT NULL,
I get the error
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [validity_date] in table [my_alarms_timeline]; found [timestamp (Types#TIMESTAMP)], but expecting [date (Types#DATE)]
This is just the reverse message of the previous one.
I also tried, instead of including hibernate-java8
, to use an AttributeConverter<LocalDate, java.sql.Date>
but this produces the same error result.
What must I do, so that my LocalDate is mapped correctly to a DATE in the DB?
I also tried with a LocalDateTime
field mapped to a TIMESTAMP, and this works without problems...
So I got it to run, by adding a columnDefinition="DATE"
to the @Column
annotation.
@Entity
@Table(name = "my_alarms_timeline", indexes = {...})
public class MyAlarm {
...
@Column(name = "validity_date", columnDefinition = "DATE")
@NotNull
private LocalDate validityDate;
}
Not sure why nobody else is seeing this issue...
这篇关于hibernate中的Java 8 LocalDate错误地映射到TIMESTAMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!