Hulk
Hulk

Reputation: 34200

Grails one to many relationship

In Grails if there is a many to many relation ships for example book has many authors

Lets says book1 has author1,autho2,author3,author4 values .Now a PUT request is sent saying that book1 has only author1,author3 then the other two values are deleted from the table.

Now for the same scenario with one to many relation ship lets say book1 has author1,autho2,author3,author4 values now if a PUT request is done with only author1,author3

Is it suppose to delete the other two i.e, author2 and author4 values?? i expect the behavior to be so..

Below is my model for book and author

class Author {
String name;
    static hasMany = [books: Book]

     static mapping = {
    books cascade: 'all-delete-orphan'
}
}

class Book{
    String name
    static belongsTo = [author: Author]
}

EDIT:

When i emplement the all-delete-orphan i get the followinh error A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance

Upvotes: 0

Views: 618

Answers (1)

micha
micha

Reputation: 49612

It depends on the cascade options set. If you are using belongsTo on the Author table grails uses cascade all by default. This means that the Author objects are removed if they are not used by any other Book. You can customize this behaviour using the cascade option (all-delete-orphan should be the value you are interested in).

Upvotes: 1

Related Questions