I have the following classes:
class User {
String name
}
class Book {
User user
}
I want that if I delete a User object, it also deletes the Book object containing the user instance. Since I have no relation from the User class to the book class cascade delete will not work.
I can write the Book class as:
class Book {
belongsTo = [user: User]
}
The former also does not do cascade delete because there is still no relation defined in the User class.
What I did is the following:
class User {
String name
def deleteUser() {
def books = Book.findAllByUser(this)
books.each { it.delete(flush: true) }
}
delete(flush: true)
}
I do not think that this is the best solution. What can I do instead? Is there a way to extend the User class delete() function somehow?
I tried the following but it fails.
def delete() {
def books = Book.findAllByUser(this)
books.each { it.delete(flush: true) }
}
super.delete(flush: true)