Null in hasMany assosiation list after removing item

后端 未结 1 1789
醉梦人生
醉梦人生 2021-01-06 07:55

There is domain object:

 class Book {
      List pictures
      static hasMany = [pictures:Picture]
      static mapping = {
        pictures          


        
相关标签:
1条回答
  • 2021-01-06 08:32

    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

    0 讨论(0)
提交回复
热议问题