0
votes

Let's say i have 3 domain classes:

class Book {
    static belongsTo = [Author,User]
    static hasMany = [authors:Author,usersWhomReadThisBook:User]
    String title
}
class Author {
    static hasMany = [books:Book]
    String name
}
class User {
    static hasMany = [booksRead:Book]
    String name
}

belongsTo defines a cascading relationship so deleting a parent will delete all objects that belong to it.

Question is: when I delete a User does it cascade up and delete the Books the user have read? Even if it still belongs to an existing Author? Or does it only delete from the join table?

The documentation is not clear on this use case.

1
convert bi-dir m2m into o2m and thus get rid of the bigger part of your problems :)injecteer
i was thinking about the same thing, but i need to know if there is a more elegant solution for the problemSandorRacz
Why not create a test application and observe the behavior?Joshua Moore
Deletes do not cascade in many-many relationships.dmahapatro
I mean m2m poses more problems than it solvesinjecteer

1 Answers

0
votes

Your question is:
When I delete a User does it cascade up and delete the Books the user have read? Even if it still belongs to an existing Author? Or does it only delete from the join table?

The answer is:No deletes are not cascaded for many to many,here if we delete the user only it will be deleted because the books is associated with other class Author.