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?
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()) }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With