I need to save and update properties of different domain classes in one transaction.
I know I can use the following:
def addToChildren(String name, int age) {
User.withTransaction {
def user = new User(name)
user.age = age
user.save()
def school = new School()
school.addToUsers(user)
school.save()
}
}
- Is it possible to use transactions like this in a domain model?
- Where should I put the addToChildren function? Is it in the User class, the School class or some external place like a Service?
- Should I use withTransaction or withNewTransaction?
- Can I have transactions in model function?
- Should addToChildren be a model function which calls a service function to modify the model properties?