1

多対多の関係を持つ 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 で削除を呼び出すと beforeDelete で POI と登録の関係が削除されますが、効果的に削除を実行しようとすると、次のエラーが発生します。

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 を使用してこの問題を解決する方法を知っている人はいますか?

4

1 に答える 1