Null in hasMany assosiation list after removing item

[亡魂溺海] 提交于 2019-12-19 03:37:24

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/30011627/null-in-hasmany-assosiation-list-after-removing-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!