删除项目后,在hasMany协作列表中删除 [英] Null in hasMany assosiation list after removing item

查看:132
本文介绍了删除项目后,在hasMany协作列表中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有域对象:

  class Book {
List< Picture> images
static hasMany = [pictures:Picture]
static mapping = {
pictures lazy:false,cache:'nonstrict-read-write'
}
}

有时,通过代码从列表中删除图片后,它会在图片列表中生成空项目。

b
$ b

  .. 
book.refresh()
def pic = Picture.get(params.id)
subject .removeFromPictures(pic)
subject.save()

看起来,GORM不会更新idx字段在assosiation表中。
我无法复制它,但我在生产服务器上获得了几次它

在我看来,它可能是二级缓存和连续修改的问题。如何防止它?



Grails 2.4.5
MariaDB

解决方案

我认为问题可能取决于您在课堂上设置的级联删除行为。
首先,在调用

  subject.removeFromPictures(pic)
subject.save()

您必须致电。

  pic.delete()

但如果问题仍然存在,您可以使用你可以添加GORM事件:

  class Book {
...
.. 。
def beforeUpdate(){
checkNulls()
}
$ b $ def beforeValidate(){
checkNulls()
}

def checkNulls(){
pictures?.removeAll(null)
}

参考: GORM活动 p>

There is domain object:

 class Book {
      List<Picture> pictures
      static hasMany = [pictures:Picture]
      static mapping = {
        pictures lazy: false, cache: 'nonstrict-read-write'
      }
    }

Sometimes, after deleting pictures from list by code it produce null item in pictures list.

..
book.refresh()
def pic = Picture.get(params.id)
subject.removeFromPictures(pic)
subject.save()

It looks like, GORM not update idx field in assosiation table. I can't reproduce it, but I got few times it on production server

In my opinion, it can be problem of second level cache and concurent modification. How to prevent it?

Grails 2.4.5 MariaDB

解决方案

i think the problem can depend on the cascade delete behaviour you set on the class. First of all, after calling

subject.removeFromPictures(pic)
subject.save()

You have to call.

pic.delete()

But if the problem persist, you can use GORM events so in your class you can add:

class Book {
...
...
def beforeUpdate(){
checkNulls()
}

def beforeValidate(){
checkNulls()
}

def checkNulls(){
pictures?.removeAll(null)
}

Ref: GORM Events

这篇关于删除项目后,在hasMany协作列表中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆