Persist a list of LocalDate with GORM

扶醉桌前 提交于 2020-01-06 16:19:22

问题


I'm trying to persist a list of joda LocalDate in grails. What I have right now is something like this:

package com.publidirecta

import org.joda.time.LocalDate

class Evento {

    List <LocalDate> fechas = []

    static hasMany = [fechas:LocalDate]
}

and I get the following error :

MappingException: Missing type or column for column[fechas_persistent_local_date] on domain[Evento] referencing[org.jadira.usertype.dateandtime.joda.PersistentLocalDate]
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . in java.lang.Thread

I tried with out the hasMany property, but doesnt work either(just didnt add anything)


回答1:


As Alidad suggested you should wrap the LocalDate with a new entity and have a on-to-many relationship with this entity. In addition you will have to take care of mapping the LocalDate type to the database since this is not a type natively supported by Hibernate. Take a look at this guide which covers the topic.

Using the User Type library your class should look something like:

package com.publidirecta

class Evento {

    static hasMany = [fechas: Fecha]

    List <Fecha> fechas = []
}

And

import org.jadira.usertype.dateandtime.joda.PersistentLocalDate
import org.joda.time.LocalDate

class Fecha {
    LocalDate date

    static mapping = {
        date type: PersistentLocalDate
    }
}

Make sure your add the following to your BuildConfig:

dependencies {
        compile 'org.jadira.usertype:usertype.jodatime:1.9'
    }



回答2:


How about defining your 'fechas' as another domain with localDate and create hasmany relationship:

something like this:

/Evento.groovy

class Evento  {
 static hasMany = [fechas: Fecha]
}

/Fecha.groovy

import org.joda.time.LocalDate

class Fecha  {

    LocalDate date

}


来源:https://stackoverflow.com/questions/15506449/persist-a-list-of-localdate-with-gorm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!