2014-09-02 50 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之类的东西来确定关系,并且反过来可以找出需要插入的东西的顺序。