12

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?

user2083529
  • 715
  • 3
  • 13
  • 25

3 Answers3

35

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()) }

}
  • Facing this error : org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.time.ZonedDateTime. Did you face the same issue? I'm with Java, MongoDB. – Nguyễn Đức Tâm Aug 17 '21 at 17:17
  • 1
    @NguyễnĐứcTâm You also need to register the converters for ZonedDateTime, like here: https://stackoverflow.com/a/51664363/4245733 – Indivon Aug 18 '21 at 09:50
  • still doesn't work for me. I use Instant type though. – Dmitry Avgustis Apr 14 '22 at 08:33
0

Old answer was mistaken, but I can't delete it because it is accepted. Please scroll to other answers.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
0

You also have to put this annotation @EntityListeners(AuditingEntityListener.class) on the class where you use the @CreatedDate.

Bunyi
  • 1