index-1-1.jpg
  • Aurora Solutions

Domain Relationships in Grails

In this article I am going to talk about how associations between domain objects are used in Grails. Grails is known as domain driven language that means we can build application using bottom to top approach which is more nearer to object oriented programming. GORM (Grail Object Relational Mapping) uses intern Hibernate to map the domain with table which gives life to Domain modeling. If we combine everything just only based on domain we could build the whole web application. Scaffolding can bring domain design directly to visible state to business users with extra involvement


Grails gives flexibility to make various combination of mapping such as one-to-one, one-to-many, many -to-many. It uses belongsTo for unidirectional and hasMany and direct reference for bidirectional. In subsequent topic I will discuss in detail uses of these properties.


One-to-one relationship:

A one-to-many relationship is the simplest one. It uses the hasOne property on the owning side. This relationship can be unidirectional or bidirectional. Lets take a simple example of a book and an author. Each book can have one author. We can write this relationship like below.

class Book{
  Long id
  String bookName
  String bookDesc

  static belongsTo=Author
}

class Author{
  Long id
  String authorName

  static hasOne =[book:Book]
}

In above example belongsTo field inform GORM that Book has a relationship to Author and eligible for cascade, so if any Author deleted, the matching Book will also be deleted. hasOne only works with bidirectional relationship.


One-to-many relationship:

One-to-many relationship occurs when one object has a multivalued relationship with another object. Grails introduces hasMany and belongsTo properties to establish one-to-many relationship. Lets take our previous example. Now in case of one-to-many relationship we say an author can have more than one book. In that case the relationship can be written as follows.

class Book{
  Long id
  String bookName
  String bookDesc
}

class Author{
  Long id
  String authorName

  static hasOne =[book:Book]
}

In the above code snippet we can see that we have unidirectional relationship. Grails will,by default, map this relationship with a join table. The default cascading behavior is to cascade saves and update but no deletes unless a belongsTo is also specified. So we will add belongsTo in the Book domain class.


Many-to-many relationship:

Grails support many-to-many relationships by defining hasMany on both sides of the relationship and having a belongsTo on the owned side of the relationship. Lets take an example of albums and the artists. An artist can have many albums and an album can have many artists. This relationship can be written in terms of GORM as follows.

class Album {
  String name

  static hasMany = [artists : Artist]
}

class Artist {
  String name

  static belongsTo = [album : Album]
  static hasMany = [album : Album]
}

In the above code snippet we are treating the ‘owning’ side (Album) just like a one-to-many, stating it has-many Artists.On the ‘owned’ side (Artist), we start just like a many-to-one by adding a belongs-to Album.Instead of giving Artist an ‘album’ property, creating a many-to-one, we give it a has-many of Album. This creates the many-to-many relationship.


Conclusion:

Domain is the heart of Grail based development which map domain as entity to database table. I hope this article will help you understand how Grails map relationships between domain objects.

0 views0 comments

Recent Posts

See All

Grails & reCAPTCHA

What is reCAPTCHA? reCAPTCHA is a powerful service to detect against spam & abuse and can very easily help your distinguish between an actual site visitor or a bot. reCAPTCHA is a free service provide

GORM Feature of Grails

This article will explain Grails Object Relational Mapping feature and Grails relationship mapping such as one-to-one, one-to-many, many –to-many. Grails is known as domain driven language that means

Grails: Validating Domain Object

In this series of articles I am going to talk about the validations or constraints that can be added to Grails domain objects so that any junk data should not get stored to the database and hence our