Jeff Storey
Jeff Storey

Reputation: 57192

Grails hasMany delete behavior

I have 2 domain classes

class A {
   static hasMany = [ b : B ]
}

class B {
   static belongsTo = A
}

I would like to keep the cascading saves, so when I save A, it updates the B's, but when I do a delete of A, I would like it to fail if any B's exist that are associated with that A. So you would have to explicitly delete all of the B's first.

I'm not sure the easiest way to do this in Grails. I could put a check in A before I delete it to verify that there are no B's - simple enough. But is there a way to control this through cascading or relationship behaviors so I don't have to put the logic in there?

Upvotes: 1

Views: 479

Answers (1)

Sudhir N
Sudhir N

Reputation: 4096

Specify the cascade behavior for the collection

class A {
   static hasMany = [ b : B ]

   static mapping = {
        b cascade: 'save-update'
    }
}

It will cascade save and update but not delete.

Upvotes: 1

Related Questions