将[hasMany:]引用的数据类型更改为列表? [英] Changing the data type of the [hasMany:] reference to a list?

查看:168
本文介绍了将[hasMany:]引用的数据类型更改为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在grails中改变静态hasMany = [myList:Stuff] 定义的数据类型?我试过

Is there a way to change the data type of the static hasMany = [myList: Stuff] definition in grails? I tried

List<Stuff> myList
hasMany = [myList : Stuff]

但我现有的测试开始投掷

but my existing tests started throwing

Stuff._MyContainer_mylistBackref;嵌套的异常是org.hibernate.PropertyValueException:非空属性引用空值或瞬态值

这表明两者不等价他们如何处理的条款。我在这里做错了什么?

which indicates the two aren't equivalent in terms of how they're being handled. What am I doing wrong here?

推荐答案

Grails手册5.2.4 ,这是使集合成为 List 的正确方法。

As described in section 5.2.4 of the Grails manual, this is the correct way to make the collection a List.

我怀疑问题在于,默认情况下,约束 nullable(false)应用于所有的域类属性,你正试图为这个属性保存一个空值。为了解决这个问题,添加一个约束条件,允许该属性为null(如果这就是你想要的)

I suspect the problem is that by default the constraint nullable(false) is applied to all domain class properties and you're trying to save a null value for this property. To fix this, add a constraint that allows this property to be null (if that's what you want

List myList
static constraints = {
    myList(nullable: true)
}

在验证/保存对象之前确保该属性不为空。

Alternatively, make sure that the property is not null before the object is validated/saved.

这篇关于将[hasMany:]引用的数据类型更改为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆