maestro maestro - 1 year ago 133
Java Question

ZonedDateTime in MongoDB through Morphia

In a nutshell:

  • java.time.ZonedDateTime
    has no args constructor (actually it has no

    constructor at all)

  • to get over this, I should write a
    for Morphia

  • if I try to save and load my
    without converter, Morphia
    saves the full
    object as
    which is cool because
    I could read all the fields and initiate a ZonedDateTime object from
    it, but I got an exception on loading:
    No usable constructor for java.time.ZonedDateTime

  • if I try to save and load the
    with the converter, I got
    another exception:
    Can't find a codec for class java.time.LocalDateTime
    . I started to play with the converters, based
    on this article but without any result.

  • So if I have no converter I can save but can't load, if I have the converter, I can't even save because of missing codecs.

Any idea?

Turned out that I should write a converter for each subtype started from
), but turned out that I should write a converter for
which is package-private.

Answer Source

I pushed support for the more common Java 8 datetime types to Morphia last week targeting the 1.3 release. See the issue here and the linked commits. If there's a type you need that's not listed, please file a Morphia issue and I'll try to make sure it's included in 1.3 as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download