java yearmonth,java.time 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;

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

解决方案

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).

Other JPA providers support persisting such types out of the box (DataNucleus JPA certainly does, and Hibernate also maybe with an optional dependency?)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值