Grails GORM - 在父母之前保存孩子

时间:2014-09-02 20:30:05

标签: hibernate grails orm parent-child one-to-many

我有几个具有子父关系的类,但是,当尝试保存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这样做似乎很愚蠢)而且我也不知道如何改变GORM& #39; s(Hibernate' s)首先保存父级的行为。

1 个答案:

答案 0 :(得分:3)

问题在于您的Request域类使用相关Form的简单属性分配。

因此,您需要使用Form form代替static hasOne = [form:Form]来定义该关系。

原因在于GORM通过使用hasOnehasMany之类的东西来确定关系,并反过来可以确定需要插入的顺序。