有以下结构制作的任何方式:Grails的hasOne和的hasMany具有相同的域和级联操作
class Parent {
String name
static hasOne = [firstChild: Child]
static hasMany = [otherChildren: Child]
}
class Child{
String name
static belongsTo = [parent: Parent]
}
现在,当我尝试运行一个简单的代码:
Parent p = new Parent(name: "parent", firstChild: new Child(name: 'child'))
p.addToOtherChildren(new Child(name: "child2"));
p.addToOtherChildren(new Child(name: "child3"));
p.save(flush: true)
它保存对象但是当我尝试在Parent上执行列表操作时,它会抛出此错误:
org.springframework.orm.hibernate4.HibernateSystemException: More than one row with the given identifier was found: 2, for class: test.Child; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 2, for class: test.Child
这里的问题是hasOne st在子项中存储Parent id,就像hasMany with belongsTo一样,现在多于一个的子实例具有相同的父项ID,因此很难确定哪一个是用于firstChild的。
我试图this solution以及但是它引发此异常:
org.springframework.dao.InvalidDataAccessApiUsageException: Not-null property references a transient value - transient instance must be saved before current operation : test.Child.parent -> test.Parent; nested exception is org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : test.Child.parent -> test.Parent
是否有这样做还是我做错了什么的没有更好的办法?
我想级联保存firstChild和其他孩子与父母。