问题描述
如何在实体中使用YearMonth?我看过很多关于转换器的堆栈溢出答案?这仍然需要,如果是这样的话?
How do you use YearMonth within an entity? I have seen a lot of stack overflow answers about a converter? Is this still needed and if so how?
@Basic(optional = false)
@NotNull
@Column(name = "cc_exp_date")
private YearMonth ccExpDate;
我得到的例外情况如下。我可以将它转换为set方法中的日期,并在实体的get方法中转换回YearMonth作为解决方法吗?
The exception I am getting is below. Could I just convert this into a date within the set method and convert back to YearMonth within the get method of the entity as a work around?
MysqlDataTruncation: Data truncation: Incorrect date value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x06\x0C\x00\x00\x07\xE2\x02x' for column 'cc_exp_date' at row 1
推荐答案
如果您的JPA提供程序没有以合理的方式保留类型(在这种情况下是因为它是一个Java8类,它是在JPA 2.1获得批准后添加的,并且因为您使用的EclipseLink最近没有太多更新来支持更新的类型),那么您需要定义一个JPA 2.1 AttributeConverter
将其转换为标准的JPA持久化类型(在这种情况下类似于 java.sql.Date
)。
If your JPA provider does not persist a type in a sensible manner (in this case because it is a Java8 class, which was added after JPA 2.1 was approved, and because you're using EclipseLink which hasn't had much updates of late to support newer types) then you need to define a JPA 2.1 AttributeConverter
to convert it to a standard JPA persistable type (in this case something like java.sql.Date
).
其他JPA提供程序支持开箱即用这些类型(DataNucleus JPA肯定会这样做,而Hibernate也可能带有可选的依赖项?)
Other JPA providers support persisting such types out of the box (DataNucleus JPA certainly does, and Hibernate also maybe with an optional dependency?)
这篇关于java.time YearMonth作为实体内的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!