My question is based on the following (simplified) Grails domain class
class Dimension {
String name
static hasMany = [
children: Dimension,
parents: Dimension
]
}
Is there a way to map the many-to-many parents/children relationship to a single join table?
As far as I know, the only way to do that is to create another domain class that represents a parent-child relationship.
The
mappedBykeywords specifies that the object refering to aDimensionDependencyis always the child. By specifyingall-delete-orphanin the mapping, we make sure that when removing aparentDependencyfrom a child, the associatedDimensionDependencyis deleted from the database.You may also add convenience methods to your
Dimensionclass to encapsulate operations onDimensionDependencies, to make the interface more GORM-like.I’ve been using this approach for some time and have had no trouble so far.