Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@CreatedBy and @LastModifiedDate are no longer working with ZonedDateTime?

i have updated an old spring boot 1.5.3 project to spring boot 2.0.0. RELEASE. I have an auditing entity with two fields of type ZonedDateTime annotated with @CreatedBy and @LastModifiedDate.

In the previous versions everything was working fine. However, with the new update, upon saving the entity in the repository i get an error i.e

 createdDate=<null>
 lastModifiedDate=<null>
 ]! Supported types are [org.joda.time.DateTime, org.joda.time.LocalDateTime, java.util.Date, java.lang.Long, long]

I checked the AnnotationAuditingMetaData, and i found noting related to ZonedDateTime.

There is also this issue in https://jira.spring.io/browse/DATAJPA-1242, I believe it's related.

My question is what am i doing wrong here, does spring stopped the support or am i doing something wrong?

like image 567
user2083529 Avatar asked Mar 08 '18 09:03

user2083529


1 Answers

I had the same problem and could solve it adding a dateTimeProviderRef to @EnableJpaAuditing.

Java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.auditing.DateTimeProvider;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import java.time.ZonedDateTime;
import java.util.Optional;

@Configuration
@EnableJpaAuditing(dateTimeProviderRef = "auditingDateTimeProvider")
public class PersistenceConfig {

    @Bean // Makes ZonedDateTime compatible with auditing fields
    public DateTimeProvider auditingDateTimeProvider() {
        return () -> Optional.of(ZonedDateTime.now());
    }

}

Kotlin

@Configuration
@EnableJpaAuditing(dateTimeProviderRef = "auditingDateTimeProvider")
class PersistenceConfig {

    @Bean // Makes ZonedDateTime compatible with auditing fields
    fun auditingDateTimeProvider()= DateTimeProvider { of(ZonedDateTime.now()) }

}
like image 148
Maximiliano De Lorenzo Avatar answered Nov 16 '22 17:11

Maximiliano De Lorenzo