Null in hasMany list of associations after deleting an element
There is a domain object:
class Book {
List<Picture> pictures
static hasMany = [pictures:Picture]
static mapping = {
pictures lazy: false, cache: 'nonstrict-read-write'
}
}
Sometimes, after removing images from the list by code, it generates a zero item in the image list.
..
book.refresh()
def pic = Picture.get(params.id)
subject.removeFromPictures(pic)
subject.save()
It looks like GORM is not updating the idx field in the sort table. I cannot reproduce it, but I got it multiple times on production server
In my opinion, this may be a problem of the second-level cache and related modifications. How can you prevent this?
Grails 2.4.5 MariaDB
+3
demon101
source
to share
1 answer
I think the problem might depend on the cascading delete behavior set in the class. First of all, after calling
subject.removeFromPictures(pic)
subject.save()
You need to call.
pic.delete()
But if the problem persists, 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
+1
Giuseppe Iacobucci
source
to share