ubiquibacon
ubiquibacon

Reputation: 10677

Is there anyway to perform a "deep save" in Grails?

In my service I create a "root" object which has associations to many objects which in turn have associations to many more objects and so on. Once the root object is completely built and ready to be saved I would like to call save on the root object and have all associated objects all the way down be saved as well. Right now I have a recursive method called deepSave which does this. Is there a better way?

Upvotes: 4

Views: 1487

Answers (1)

micha
micha

Reputation: 49612

If you use belongsTo GORM automatically defines the cascading for you. This means: If A belongsTo B then A will be saved when B is saved. However, it is possible to define cascading without using belongsTo (if this does not fit to your domain model):

class Author {    
  static hasMany = [books: Book]
  static mapping = { books cascade: 'all-delete-orphan' } 
}

You should have a look at the cascade property provided by GORM. Additionaly the hibernate documentation provides more detailed information.

Upvotes: 6

Related Questions