Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Grails makes creating web applications in Java easier. It helps in reducing the complexity of creating a web application in Java. Grails achieve this by using already existing technologies such as Hibernate and Spring.
In the course of the article, we will see what domain classes are, how we can create them and how we can customize them. Let us start with understanding GORM.
GORM
It stands for Grails’ Object Relational Mapping. It is a data access framework. It simplifies our work of writing code for accessing data from any database.
Now to use GORM for Hibernate in Grails, we need to add the following in the ‘build.gradle’ file:
Note: The above lines of code may change according to the version of Grails we use. The above lines will work for Grails 3.
Now let us see domain classes and how we can create one.
Domain Class
Domain classes serve as essential classes. They are the core class of any application. Domain classes contain information about the process, and they also implement behaviour.
Multiple domain classes share either one-one relationships, many-to-one relationships, or many-to-many.
We have an option of writing the domain classes in Java. The following code represents a GORM in action:
def fruit = Fruit.findByTitle("Citrus Fruits")
fruit
.addToName(name:"Orange")
.addToName(name:"Kiwi")
.save()
Let us now move on to creating a domain class.
Creating a Domain Class
We have a command create-domain-class which is used to create the domain class.
The line below is the command to create a class in a particular package.
The above line will create a class with the name DClass at the location grails-app/domain/Coding Ninjas Studio/DClass.groovy. ‘Coding Ninjas Studio’ here is the package's name and ‘DClass’ is the name of the class. The class will look like this:
package Coding Ninjas Studio
class DClass {
}
Now the above lines will create a class with no member variables. We can add member variables according to our needs. For example, we can customize the above class in the following manner:
class DClass {
String product
Integer quantity
String bought
}
Now that we have a domain class, let us see how we can use or manipulate it.
We use the shell or console to manipulate the domain class. Following is a console command for grails:
grails console
This statement will open up an interactive GUI to execute Groovy commands.
Now let us see the CRUD operations.
CRUD Operations
CRUD operations include creating, reading, updating, and deleting operations.
Let us see all of this one by one:
Create
This operation lets us create a domain class and set the properties.
For example:
def ob= new DClass(product: "P1", quantity: 1, bought: "Yes")
ob.save()
The save method will save the class to the database.
Read
When we create a domain class, Grails adds an id to the class. Using this id, we can retrieve the class.
For example:
def ob = DClass.get(1)
assert 1 == ob.id
The read operation uses the get method, which receives an identifier that reads DClass from the database. We also have a method to load the object in read-only mode.
def ob = DClass.read(1)
Also, if we do not want the actual object, we can have the proxy of the object using:
def ob = DClass.load(1)
Update
It is helpful in cases where we want to change something in the class. We can update the domain class and save it back to the database.
def ob = DClass.get(1)
ob.bought = "No"
ob.save()
Delete
To delete the instance, we use the delete method.
def ob = DClass.get(1)
ob.delete()
Frequently Asked Questions
What are Domain Classes?
Domain classes serve as important classes. They are the core class of any application. Domain classes contain information about the process, and they also implement behaviour.
Can we write the domain classes in Java?
Yes, we can write the domain classes in Java.
What if we do not specify a package name while creating a domain class in Grails?
If we do not specify the package name, the application name is used as the package name.
Why do we use the save() method after create and update operation?
After performing the create and update operation, the save method is used to save the newer instance to the database.
Conclusion
In this article, we learned about GORM. We saw what Domain classes are, how we can create one, and how we can customize it according to the demands. We also learned to perform the basic CRUD operations on the classes.