我的模型需要有多个相同类型的枚举:
class Broker {
static constraints = {
brokerTypes(nullable:false)
}
List<BrokerType> brokerTypes
}
模型正在使用请求中的params进行实例化,其中包含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替代方案是代替存储枚举列表,存储字符串或其他东西的列表,然后在需要时映射回枚举,但这看起来不干净
答案 0 :(得分:3)
您必须使用hasMany
子句,以便grails / gorm初始化一对多关系
您应该将以下代码段添加到您的域类中。
static hasMany = [brokerTypes : BrokerType]