我有2域类具有多对多的关系。 当我删除属于其他实体,我不得不以避免外键错误之前删除的关系。
我愿把这个代码在beforeDelete事件,但我获得与optimistc锁定问题。 这是该领域类的代码:
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与注册之间的关系被删除,当我打电话删除的是POI的beforeDelete,但是当它试图有效地执行删除,我有以下错误:
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来解决这个问题?