Search code examples
grailsgrails-ormjava-timegrails-3.0

Grails 3 and Java 8 time support


In grails 3 application, I see that new java.time classes are persisted to database. Dynamic queries and create criteria works. However, I am wondering if there is some better way how to store these time data to database. When I look to database I see some binary data, it seems it just serialize the time objects.

Is there any similar plugin as joda-time-plugin or is there any way how to configure database mapping for java.time classes?


Solution

  • Edit: Grails 3.1.1 has hibernate 5 so this is not an issue anymore...

    Grails

    I finally found a solution. First of all hibernate 5 implements persistent for java 8 time (https://hibernate.atlassian.net/browse/HHH-8844). However, grails 3 uses hibernate 4 and I haven't found any plans to upgrade hibernate in grails.

    There is an implementation for java 8 time hibernate user types (jadira extended package) which can be used similarly as in joda time plugin (see http://gpc.github.io/joda-time/guide/persistence.html). For me only version 3.1.0.GA and lower worked.

    build.gradle

    dependencies {
        compile "org.jadira.usertype:usertype.extended:3.1.0.GA"
    }
    

    application.groovy

    grails.gorm.default.mapping = {
        "user-type" type: org.jadira.usertype.dateandtime.threeten.PersistentInstantAsMillisLong, class: java.time.Instant
    }