2014-09-02 45 views
0

我有幾個類,有一個孩子父母的關係,但是,試圖挽救一切的Grails GORM首先保存了孩子,這最終引發了以下錯誤時:的Grails GORM - 父母之前保存孩子

ORA-02291: integrity constraint violated - parent key not found 

這裏是我的課的基本代碼表示:

class Request 
{ 
    // Mapping definitions 
    static mapping = { 
    table(name: 'FORMS_REQUEST') 
    tablePerHierarchy(false) 
    id(length: 20, precision: 20, scale: 0, generator: 'sequence', params: [sequence: 'FORMS_REQUEST_SEQ'])  
    } 

    // Properties 
    Timestamp version 
    Form form 
    Date submittedTime 
} 

abstract class Form 
{ 
    // Mapping definitions 
    static mapping = { 
    table(name: 'FORMS_FORM') 
    tablePerHierarchy(false) 
    id(length: 20, precision: 20, scale: 0, generator: 'sequence', params: [sequence: 'FORMS_FORM_SEQ']) 
    } 

    // Relationship definitions 
    static belongsTo = [request: Request] 

    // Properties 
    Timestamp version 
} 

class AccessForm extends Form 
{ 
    // Mapping definitions 
    static mapping = { 
    table(name: 'FORMS_ACCESS_FORM') 
    id(length: 20, precision: 20, scale: 0, generator: 'sequence', params: [sequence: 'FORMS_ACCESS_FORM_SEQ']) 
    } 

    // Relationship definitions 
    static hasMany = [adGroups: AccessFormAD, printers: AccessFormPrinter] 

    // Properties 
    List adGroups 
    List printers 
} 

class AccessFormAD 
{ 
    // Mapping definitions 
    static mapping = { 
    table(name: 'FORMS_ACCESS_FORM_AD') 
    id(length: 20, precision: 20, scale: 0, generator: 'sequence', params: [sequence: 'FORMS_ACCESS_FORM_AD_SEQ']) 
    } 

    // Relationship definitions 
    static belongsTo = [accessForm: AccessForm] 

    // Properties 
    Timestamp version 
} 

class AccessFormPrinter 
{ 
    // Mapping definitions 
    static mapping = { 
    table(name: 'FORMS_ACCESS_FORM_PRINTER') 
    id(length: 20, precision: 20, scale: 0, generator: 'sequence', params: [sequence: 'FORMS_ACCESS_FORM_PRINTER_SEQ']) 
    } 

    // Relationship definitions 
    static belongsTo = [accessForm: AccessForm] 

    // Properties 
    Timestamp version 
} 

用正確的數據創建的所有類等,並調用後,以下:

request.save(flush: true) 

我得到上述錯誤。日誌顯示在Hibernate下面的SQL語句:

Hibernate: select FORMS_REQUEST_SEQ.nextval from dual 
Hibernate: select FORMS_FORM_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: select FORMS_ACCESS_FORM_AD_SEQ.nextval from dual 
Hibernate: insert into FORMS_ACCESS_FORM_AD (version, checked, DN, type, access_form_id, ad_groups_idx, id) values (?, ?, ?, ?, ?, ?, ?) 
Sep. 02 2014 @ 03:58:06 PM - class spi.SqlExceptionHelper - ORA-02291: integrity constraint (FK_954TU4QUPD4QE7H72XGVXSTKV) violated - parent key not found 

它首先搶救孩子的對象之前的父母,這可以解釋的錯誤,但是,我不知道爲什麼(似乎愚蠢的格姆做這個),我也不知道如何改變GORM(Hibernate)的行爲來首先保存父項。

+0

我猜這是因爲您的頂級請求正在使用'Form form'而不是'static hasOne = [form:Form]'。 – 2014-09-02 20:33:11

+0

有趣的...修復它。沒想到這是必需的(因爲只有當您想要更改哪個表具有外鍵時,才需要這種關係)。 – 2014-09-02 20:42:33

回答

3

問題是,您的Request域類正在使用簡單的屬性分配爲相關的Form

因此,而不是Form form您需要使用static hasOne = [form:Form]來定義該關係。

原因是GORM通過使用諸如hasOnehasMany之類的東西來確定關係,並且反過來可以找出需要插入的東西的順序。