让Springboot JpaAuditing 支持ZonedDateTime

我们项目需要支持国际化,那么日期时间就需要有时区了,否则我们在今天早上9点干的事,人家美国人看到的是明天的时间。所以我们在Auditable中的创建时间和更新时间我们都定义为ZonedDateTime.
然而在保存的时候却抛出如下错误:

Caused by: java.lang.IllegalArgumentException: Cannot convert unsupported date type java.time.LocalDateTime to java.time.ZonedDateTime; Supported types are [java.time.LocalDateTime, java.time.LocalDate, java.time.LocalTime, java.time.Instant, java.util.Date, java.lang.Long, long]
	at org.springframework.data.auditing.DefaultAuditableBeanWrapperFactory.rejectUnsupportedType(DefaultAuditableBeanWrapperFactory.java:235)
	at org.springframework.data.auditing.DefaultAuditableBeanWrapperFactory$DateConvertingAuditableBeanWrapper.getDateValueToSet(DefaultAuditableBeanWrapperFactory.java:201)
	at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory$MappingMetadataAuditableBeanWrapper.lambda$setDateProperty$1(MappingAuditableBeanWrapperFactory.java:244)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory$MappingMetadataAuditableBeanWrapper.setDateProperty(MappingAuditableBeanWrapperFactory.java:240)
	at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory$MappingMetadataAuditableBeanWrapper.setLastModifiedDate(MappingAuditableBeanWrapperFactory.java:221)
	at java.base/java.util.Optional.ifPresent(Optional.java:178)
	at org.springframework.data.auditing.AuditingHandlerSupport.touchDate(AuditingHandlerSupport.java:193)
	at org.springframework.data.auditing.AuditingHandlerSupport.lambda$touch$0(AuditingHandlerSupport.java:137)
	at java.base/java.util.Optional.map(Optional.java:260)
	at org.springframework.data.auditing.AuditingHandlerSupport.touch(AuditingHandlerSupport.java:134)
	at org.springframework.data.auditing.AuditingHandlerSupport.markModified(AuditingHandlerSupport.java:127)
	at org.springframework.data.auditing.AuditingHandler.markModified(AuditingHandler.java:98)
	at org.springframework.data.jpa.domain.support.AuditingEntityListener.touchForUpdate(AuditingEntityListener.java:112)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.hibernate.jpa.event.internal.ListenerCallback.performCallback(ListenerCallback.java:55)
	at org.hibernate.jpa.event.internal.CallbackRegistryImpl.callback(CallbackRegistryImpl.java:123)
	at org.hibernate.jpa.event.internal.CallbackRegistryImpl.preUpdate(CallbackRegistryImpl.java:86)
	at org.hibernate.event.internal.DefaultFlushEntityEventListener.invokeInterceptor(DefaultFlushEntityEventListener.java:346)
	at org.hibernate.event.internal.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:327)
	at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:244)
	at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
	at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
	at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:226)
	at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:90)
	at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:40)
	at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
	at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1403)
	at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:484)
	at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2319)
	at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:1976)

看来Springboot-jpa是不支持ZonedDateTime,网上搜了一下没找到有用的东西,于是自己解决吧。跟踪调试发现,AuditorAware 里面的当前时间是由一个DateTimeProvider生成的,而这个缺省的DateTimeProvider就是调用LocalDateTime.now()来生成的,那么我就自己创建一个DateTimeProvider,调用ZonedDateTime.now()试试,代码如下:

@Configuration
@EnableJpaAuditing(auditorAwareRef = "IrmpAuditorAware", dateTimeProviderRef = "zonedDateTimeProvider")
public class AuditorAwareConfig {

    @Bean("IrmpAuditorAware")
    AuditorAware<String> create() {
        return () -> Optional.of(AuthUtils.getLoginUsername());
    }

    @Component("zonedDateTimeProvider")
    public class CustomDateTimeProvider implements DateTimeProvider {
        @Override
        public Optional<TemporalAccessor> getNow() {
            return Optional.of(ZonedDateTime.now());
        }
    }
}

再测试一下,果然正常了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值