Changing the data type of the [hasMany:] reference to a list?

南笙酒味 提交于 2019-12-22 12:54:05

问题


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; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/5379345/changing-the-data-type-of-the-hasmany-reference-to-a-list

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