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 we can build application using bottom to top approach which is more nearer to object oriented programming. GORM uses internal Hibernate to map the domain class with table which gives life to Domain modeling. If we combined everything just only based on domain we could build the whole web application. Scaffolding can bring domain design directly to visible state to users.
But keep in mind Grails is an agile development framework. That means we build application quickly, deploy and run in one shot and make changes at any layer without impacting other parts such UI, domain modeling, service classes etc.
Create Domain Class
Grails domain classes are the most important of the application.This is like the root of application which lets us visualize the whole application. We can use it as entity object for mapping to database.
package firstapp.domain.com.User class User { long userId String userName String password } We don't need to worry about setter and getter methods as Grails dynamically generates them. By default, all the fields in a domain class are persisted to the database. The User class contains two fields’ username and password which will map to table User with column name username and password. GORM provides following dynamic methods to access database without exploring the PL/SQL directly
User.save() //saves the data to the User table in the database.
User.delete() //deletes the data from the User table.
User.list() //returns a list of Users.
User.get() //returns a single User .
Relationship
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. hasMany also uses for many relationship. In subsequent topic we will discuss in detail uses of these properties.
One-to-one relationships
One-to-one relationships could be unidirectional or bidirectional with single valued reference. For example User has single address and each address belongs to only one user. Following figure illustrates that a one-to-one relationship between user and his address.
Figure Assigning one-to-one on entities
For defining relationship between user and address first we will create domain class as follows
We will start creating
In below 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. We can define One-to-one relationship unidirectional or bidirectional by defining belongsTo on Address object and adding Address reference on User (owner) object.
package com.onetoone.domain class Book { Long id String bookName String bookDesc static belongsTo=Author //Book belong to Author } One-to-many Relationship One-to-many relationship occurs when one object has a multivalued relationship with another object. For example one-to-many relationship exists between employee and department. Below figure depicts that a department can have many employees, but that each individual employee can work for only one department Figure : Assigning one-to-many on entities
Grails introduces hasMany and belongsTo properties to establish one-to-many relationship. For e.g. hasMany need to define on Department domain and belongsTo need to define on Employee domain class as follows: package com.onetomany.domain
class Department {
long id
String departmentName
static hasMany=[employees:Employee]
}
package com.onetomany.domain
class Employee {
long id
String employeeName
String emailId
String phone
static belongsTo=Department
}
Many-to-many relationship
If two objects are in a many-to-many relationship, they will both have a hasMany
clause pointing to the other object.
Lets do Modeling a Post that can have many Tags
class Post { String content Date dateCreated static constraints = { content(blank: false) } static mapping = { sort dateCreated:"desc" } static hasMany = [ tags : Tag ] } This will model post with many tags.
class Tag {
String name
static constraints = {
name(blank: false)
}
static hasMany = [ posts : Post ]
static belongsTo = [ Post ]
}
We can see the hasMany relationship that is linking back to the Post
object.
Conclusion
Domain is the heart of Grail based development which map domain as entity to database table without configuration or annotation .
Comments