list - 带有无法在GORM/Grails中使用的枚举列表的持久域模型

标签 list grails groovy enums gorm

我的模型需要具有相同类型的多个枚举:

class Broker {

    static constraints = {
        brokerTypes(nullable:false)
    }

    List<BrokerType> brokerTypes
}

正在使用来自请求的参数实例化该模型,该模型中包含一个BrokerTypes列表:
def save(){
        def brokerInstance = new Broker(newParams)
        System.out.println(brokerInstance.getBrokerTypes().toString());
        if (!brokerInstance.save(flush: true)) {
            render(view: "create", model: [brokerInstance: brokerInstance])
            return
        }
        redirect(action: "show", id: brokerInstance.id)
}

println按预期打印出BrokerTypes列表,因此我知道它存在于实例中。稍后,按以下方式检索模型:
def brokerInstance = Broker.findByLatAndLon(lat,lon)
System.out.println(brokerInstance.getBrokerTypes().toString());

这次,println打印出“null”

所以我想问题是GORM不知道如何存储此枚举列表,而是当调用brokerInstance.save()时,它将brokerTypes字段保存为null。

我是否需要以某种方式创建映射以使GORM识别该列表? hack的替代方法是代替存储枚举列表,而是存储字符串列表或其他内容,然后在需要时映射回枚举,但这似乎并不干净。

最佳答案

您将必须使用hasMany子句,以便grails / gorm初始化一对多关系

您应将以下代码段添加到您的域类中。

   static hasMany = [brokerTypes : BrokerType]

关于list - 带有无法在GORM/Grails中使用的枚举列表的持久域模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9191790/

相关文章:

grails - 如果提交表单标签是 uploadForm,为什么 Controller 中的更新方法不起作用

python - 使用 WSDiscovery 从服务列表中获取特定字符串

c# - 具有多个类类型的单个组合 C# 列表

hibernate - Grails & HibernateException : connnection proxy not usable after transaction completion

groovy - renderArgs 在模板中为空!框架

jenkins - 如何将 groovy 变量传递给 shell block jenkins

jenkins - 使用 Jenkins 评估 Groovy 脚本时出现 EnvInject 错误

python - 如何使用 python、pandas 修改(字符串数据列表为单个字符串)数据系列的每个元素?

python - 通过字典访问函数

grails - Grails:Spring Security Core如何在两个不同的屏幕和故障屏幕上登录用户