Spring数据jpa扩展了Jsr310JpaConverters

时间:2016-12-27 09:33:01

标签: java spring jpa spring-boot spring-data-jpa

我使用的是春季启动,而且我的jsr310日期转换有问题。 在我的实体中,我有一个ZonedDateTime,在我的数据库中有一个时间戳。 我尝试扩展Jsr310JpaConverters类并添加此转换器

@Converter(autoApply = true)
    public static class ZonedDateTimeConverter implements     AttributeConverter<ZonedDateTime, Timestamp> {

    @Override
    public Timestamp convertToDatabaseColumn(final ZonedDateTime locDateTime) {

        return (locDateTime == null ? null : Timestamp.from(locDateTime.toInstant()));
    }

    @Override
    public ZonedDateTime convertToEntityAttribute(final Timestamp sqlTimestamp) {
        return (sqlTimestamp == null ? null : ZonedDateTime.ofInstant(sqlTimestamp.toInstant(), TimeZone.getDefault().toZoneId()));
    }

}

但是当我尝试从db获取实体时我有这个错误

java.lang.IllegalArgumentException: Can not set java.time.ZonedDateTime field it.advansys.gestionalebdf.common.BaseEntity.creationdate to java.sql.Timestamp
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:38)

我也在我的Application.class

上添加了这个
@EntityScan(basePackages = "my.package.entity", basePackageClasses = MyCustomConverter.class)
有人能帮帮我吗? 感谢

编辑: 我有问题的实体财产就是这个。

@CreatedDate
@Column(name = "`CREATIONDATE`", updatable = false, nullable = false)
@Type(type = "java.sql.Timestamp")
@Temporal(TemporalType.TIMESTAMP)
@NotNull
private ZonedDateTime creationdate;

0 个答案:

没有答案