Skip to content
Advertisement

how to resolve Joda date/time type `org.joda.time.DateTime` not supported by default

I have an existing web-app and unfortunately the ‘time’ fields in the DB are not converted to zulu time. Instead we are using org.joda.time.DateTime as our datetime and timezone encapsulator.

I am in the process of updating from hibernate 4.3.7.Final to 5.3.20.Final
Also, we are letting hibernate manage the translation of these DateTime objects to/from the db using

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime lastUpdatedTimestamp; // <-- org.joda.time.DateTime

Running this on hibernate 5 gives this error:

10:22:11,463 ERROR [stderr] (default task-2) com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Joda date/time type `org.joda.time.DateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-joda" to enable handling (through reference chain: org.<company>.crs.model.PermissionEntity["lastUpdatedTimestamp"])

wondering why this would work in hibernate 4 before the update to 5? I am using joda-time version 2.1 but have been able to reproduce the issue in 2.9.7

I have also looked and found this thread Persist Joda-time’s DateTime via Hibernate but, their solution was to convert to Joda’s LocalDateTime which unless I am mistaken does not support time zones which are needed for my use case.

Edit: I have added the requested module, and I still get the same error. I have tested this with Joda versions: 2.10.10, 2.9.7, and 2.1

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>2.12.3</version>
</dependency>

Advertisement

Answer

You have to register the JodaModule on your ObjectMapper, like this:

 import com.fasterxml.jackson.datatype.joda.JodaModule;

 ObjectMapper mapper = new ObjectMapper()
    .registerModule(new JodaModule())
    ...;

Prior to Jackson 2.12, you wouldn’t get the error you’re seeing because Jackson would default to using the BeanSerializer if it encountered a Joda object. Here’s the code added in 2.12 that now raises an error instead:

https://github.com/FasterXML/jackson-databind/blob/2.13/src/main/java/com/fasterxml/jackson/databind/util/BeanUtil.java#L288

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement