5.2.1.3 Varios-a-varios - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
5.2.1.3 Varios-a-varios
Grails supports many-to-many relationships by defining a
Grails tiene soporte para relaciones varios-a-varios mediante la inclusión de hasMany on both sides of the relationship and having a belongsTo on the owned side of the relationship:
hasMany en ambos lados de la relación y del lado que expresa ser la propiedad se incluye belongsTo:class Book {
static belongsTo = Author
static hasMany = [authors:Author]
String title
}class Author {
static hasMany = [books:Book]
String name
}
Grails maps a many-to-many using a join table at the database level. The owning side of the relationship, in this case
A nivel de la base de datos Grails mapea una relación varios-a-varios mediante una tabla de unión. El lado propietario de la relación, en este caso Author, takes responsibility for persisting the relationship and is the only side that can cascade saves across.
Author, asume la responsabilidad de la persistencia de la relación y es el único que puede propagar la actualización.
For example this will work and cascade saves:
Por ejemplo, esto propagaría la creación de manera correcta:new Author(name:"Stephen King") .addToBooks(new Book(title:"The Stand")) .addToBooks(new Book(title:"The Shining")) .save()
However this will only save the
Sin embargo, esto sólo guardaría a Book and not the authors!
Book y no a los autores!new Book(name:"Groovy in Action") .addToAuthors(new Author(name:"Dierk Koenig")) .addToAuthors(new Author(name:"Guillaume Laforge")) .save()
This is the expected behaviour as, just like Hibernate, only one side of a many-to-many can take responsibility for managing the relationship.
Este es el comportamiento esperado, al igual que en Hibernate, sólo un lado de la relación varios-a-varios puede asumir la responsabilidad de la gestión.Grails' Scaffolding feature does not currently support many-to-many relationship and hence you must write the code to manage the relationship yourselfActualmente el Scaffolding de Grails no es compatible con relaciones del tipo varios-a-varios, y por lo tanto, usted deberá escribir el código para manejar este tipo de relación.

