2013-02-15 98 views
0

我想模擬一個父母/孩子的關係。如何設置域對象創建的版本屬性?

我有以下域類:

package test 
class Person { 
Person mother 
Person father 
String name 
static hasOne = [father: Person, mother: Person] 
    static constraints = { 
     name() 
     father(nullable:true) 
     mother(nullable:true) 
    } 
    def Set<Person> children(){ 
     return Person.findAllByMother(this) 
    } 
} 

我已經完成生成,所有

但是,如果我嘗試創建一個新的人,我得到以下錯誤:

Message: Parameter "#2" is not set; SQL statement: 
insert into person (id, version, father_id, mother_id, name) values (null, ?, ?, ?, ?) [90012-164] 
    Line | Method 
->> 329 | getJdbcSQLException in org.h2.message.DbException 

應該在哪裏生成版本參數?我認爲這應該在保存通話期間自動產生。

更新:這個問題似乎與父母關係有關,因爲刪除它並重新生成視圖意味着元素保持不變。

+1

你能添加一段代碼來處理新人的保存嗎? – lucke84 2013-02-15 16:36:54

+0

你是對的,這應該是自動填充的。你有沒有嘗試過「grails clean」? – david 2013-02-16 05:16:25

+0

@David是的,我嘗試了「grails clean」的各種組合。我解決了它 - 看到答案。 – Tim 2013-02-18 07:12:15

回答

0

這個問題似乎與我試圖在課堂上創建一個雙向關係有關。實際上這不是必需的。有一個單向關係人 - >父親和人 - >母親。倒數歲以下的兒童計算

我的最後一類是(我應擴展到包括父親爲好。):

package test 
class Person { 
    int id 
    Person mother 
    Person father 
    String name 
    static constraints = { 
     name() 
     father(nullable:true) 
     mother(nullable:true) 
    } 

    def Set<Person> children(){ 
     return Person.findAllByMother(this) 
    } 
} 

我還是新來的Grails。