【发布时间】:2014-10-27 05:14:13
【问题描述】:
我有几个具有子父关系的类,但是,当尝试保存所有内容时,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(Hibernate's ) 行为首先保存父级。
【问题讨论】:
-
我猜这是因为您的顶级请求使用的是
Form form而不是static hasOne = [form:Form]。 -
有趣的......修复了它。不认为这是必需的(因为当您想更改哪个表具有外键时,只有一对一关系才需要)。
标签: hibernate grails orm parent-child one-to-many