有以下結構製作的任何方式: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和其他孩子與父母。