Once again: Difference between hasOne / has Many and belongsTo/belongsToMany | realestateforms.info
toString() } Bidirectional One-to-One Relationships While you can simply treat a one-to-one relationship as two has-one relationships, that is usually not the best . ###One-to-one relationship: You, as a User, can have one (hasOne) Profile. And of course the inverse also applies. Profile (belongsTo) a User. GORM (Grails Object Relational Mapping) is a plugin integrated with Grails and based on Java ORM hasOne only works with bidirectional relationships.
Grails domain class associations - realestateforms.info
In this case, GORM does not create a join table. The bidirectional relationship is mapped through a straight foreign key on the article table: A few examples of the referring to the User: Add an article property and the unique constraint to the User domain: It means that you must have a belongsTo in the Article class with back reference to the User class.
If it's missing, you'll get the following exception during project compilation: Specify the other side of the relationship!
Note that the difference between those two cases is that GORM puts a foreign key to different tables: Many-to-many Many-to-many relationships in Grails can be specified by defining a hasMany property on both sides and having a belongsTo on the owned side: These fields are foreign keys.
One-to-many In the first example, an Author has many Books, producing a one-to-many uni-directional relationship. Uni-directional meaning that an Author has a reference to its Books, however, a Book has no clue about its Author. The association is created with the hasMany static property in the Author domain class.
Grails domain class associations
The join table has two foreign key columns: It's worth nothing, since the Book does not have a reference to its Author, it's possible for multiple Authors to have the same Book. To have Grails enforce that you want a Book to have a single Author, you'll need a bi-directional one-to-many association.
Bi-directional With a bi-directional association, a Book is given a property which references its Author. This is done by adding a belongsTo static property to the Book class. Profile belongsTo a User. A user can't have more than one profile and a profile can't belong to multiple users. You as a User, can have many hasMany Task s. The inverse would be Task s belongsTo a User. You as a User can belong to many belongsToMany i.
The inverse would be that a Group can also belong to many belongsToMany User s. That means that a user can join as many groups as he wants, and of course a single group can have unlimited users inside. There are more examples, but the best would be if you check the documentation. Try using different approaches and see what results you will get.
This cannot be undone. Cancel ddelucia replied 4 years ago pogachar said: I'll explain with a different example.