gpt4 book ai didi

hibernate - Grails GORM-在 parent 之前救助 child

转载 作者:行者123 更新时间:2023-12-02 15:00:32 27 4
gpt4 key购买 nike

我有几个具有子级父级关系的类,但是,当尝试保存所有内容时,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

它首先将子对象保存在 parent 之前,这将解释该错误,但是,我不知道为什么(GORM这样做很愚蠢),而且我也不知道如何将GORM(休眠)的行为更改为首先保存 parent 。

最佳答案

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

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

原因是GORM通过使用hasOnehasMany之类的东西来确定关系,进而可以确定需要插入什么顺序。

关于hibernate - Grails GORM-在 parent 之前救助 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25631852/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com