Search code examples
grailstypeshsqldb

which datatype for using a time in gorm?


how can i handle a time (like 14:33) during calculations and savings in database? What datatype can i use for this in a HSQL DB?

I want to calculate things like workload or rest time. Is there a built in possibility to convert times to decimals?

Thanks!


Solution

  • As for the time representation, the most convenient type ought to be Joda Time's LocalTime.
    There are several converters available to map the LocalTime type to the database.

    You need the JAR packages joda-time and joda-time-hibernate.

    Your domain class then might look like this:

    import org.joda.time.contrib.hibernate.PersistentLocalTimeAsTime
    import org.joda.time.LocalTime
    
    class TimeDomain {
        LocalTime localTime
    
        static mapping = {
            localTime type: PersistentLocalTimeAsTime
        }
    
        // business logic would belong to a service class
        LocalTime plusMinutes(int minutes) {
            localTime.plusMinutes(minutes)
        }
    }