grails beforeDelete与多对多关系
我有两个具有多对多关系的域类。当我删除属于另一个实体的实体时,我必须先删除该关系,以避免外键错误 我想将此代码放入beforeDelete事件中,但我发现optimistc锁定有问题。这是域类的代码:grails beforeDelete与多对多关系,grails,many-to-many,gorm,cascading-deletes,Grails,Many To Many,Gorm,Cascading Deletes,我有两个具有多对多关系的域类。当我删除属于另一个实体的实体时,我必须先删除该关系,以避免外键错误 我想将此代码放入beforeDelete事件中,但我发现optimistc锁定有问题。这是域类的代码: class POI { static belongsTo = [Registration]; static hasMany = [registrations: Registration] def beforeDelete = { def poiId
class POI {
static belongsTo = [Registration];
static hasMany = [registrations: Registration]
def beforeDelete = {
def poiId = this.id
POI.withNewSession { session ->
def regs = Registration.withCriteria{
pois{
idEq(this.id)
}
}
def poi = POI.get(poiId)
if(poi != null && regs.size() > 0){
regs.each{
it.removeFromPois(poi)
}
poi.save(flush: true)
}
}
}
}
}
class Registration {
static hasMany=[pois: POI];
}
因此,POI和注册之间的关系被删除,在beforeDelete中,当我对POI调用delete时,但当它尝试有效执行删除时,我出现以下错误:
optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException:
Row was updated or deleted by another transaction (or unsaved-value mapping was
incorrect): [ambienticwebsite.POI#22]
任何人都知道如何使用beforeDelete来解决这个问题吗?在大多数情况下,在GORM中,处理多对多关系而不手动创建类来表示联接表会带来很多麻烦 这方面的一个例子是 多对多示例中,删除任意一端也会删除连接条目:
class POI {
def beforeDelete() {
RegistrationPOI.withNewSession {
def rps = RegistrationPOI.findByPOI(this)
rps.each { it.delete(flush: true) } // flush is necessary
}
}
/* example convenience method to get directly
* from a POI to the associated Registrations */
Collection<Registration> getRegistrations() {
RegistrationPOI.findByPOI(this)
}
}
class Registration {
def beforeDelete() {
RegistrationPOI.withNewSession {
def rps = RegistrationPOI.findByRegistration(this)
rps.each { it.delete(flush: true) } // flush is necessary
}
}
}
class RegistrationPOI {
Registration registration
POI poi
}
类POI{
def beforeDelete(){
注册POI.withNewSession{
def rps=RegistrationPOI.findByPOI(此)
rps.each{it.delete(flush:true)}//flush是必需的
}
}
/*示例直接获取的简便方法
*从POI到相关注册*/
集合getRegistrations(){
注册POI.findByPOI(本)
}
}
班级注册{
def beforeDelete(){
注册POI.withNewSession{
def rps=RegistrationPOI.FindByrRegistration(此)
rps.each{it.delete(flush:true)}//flush是必需的
}
}
}
类别注册{
登记
波波
}
在大多数情况下,使用GORM处理多对多关系而不手动创建类来表示联接表会带来很多麻烦
这方面的一个例子是
多对多示例中,删除任意一端也会删除连接条目:
class POI {
def beforeDelete() {
RegistrationPOI.withNewSession {
def rps = RegistrationPOI.findByPOI(this)
rps.each { it.delete(flush: true) } // flush is necessary
}
}
/* example convenience method to get directly
* from a POI to the associated Registrations */
Collection<Registration> getRegistrations() {
RegistrationPOI.findByPOI(this)
}
}
class Registration {
def beforeDelete() {
RegistrationPOI.withNewSession {
def rps = RegistrationPOI.findByRegistration(this)
rps.each { it.delete(flush: true) } // flush is necessary
}
}
}
class RegistrationPOI {
Registration registration
POI poi
}
类POI{
def beforeDelete(){
注册POI.withNewSession{
def rps=RegistrationPOI.findByPOI(此)
rps.each{it.delete(flush:true)}//flush是必需的
}
}
/*示例直接获取的简便方法
*从POI到相关注册*/
集合getRegistrations(){
注册POI.findByPOI(本)
}
}
班级注册{
def beforeDelete(){
注册POI.withNewSession{
def rps=RegistrationPOI.FindByrRegistration(此)
rps.each{it.delete(flush:true)}//flush是必需的
}
}
}
类别注册{
登记
波波
}
在删除之前,您是否可以在内部不刷新的情况下进行尝试?只需使用poi.save()
。它不起作用,因为我获得了外键错误,removeFrom未保存到数据库。在删除之前,您可以尝试不清除内部的吗?只需使用poi.save()
。它不起作用,因为我获得了外键错误,removeFrom没有保存到数据库中