RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Build Your First Grails Project: A Grails-Powered Blog : Page 2

Need to clear out the clutter of your Java web development? Say hello to coding by convention with Grails.

Building the Blog, Domain First
Grails provides a coding-by-convention approach to Hibernate with an object-relational mapping (ORM) solution called the Grails Object-Relational Mapping (GORM). Through GORM, Groovy classes that are created in the domain directory are automatically picked up by Hibernate and can be persisted to and retrieved from the underlying database.

To build the blog application in Grails you need to start with a few simple domain objects. As the essence of a blog is a post, create a Post object first. Go to the command line in your project location and run this:

grails create-domain-class Post

You will notice that a Groovy class called Post.groovy has been created under the grails-app\domain directory. With your domain object created, you next need to add the necessary properties to it:

  • Title
  • Teaser
  • Content
  • Last modified
  • Published

Assuming that you also want to allow people to make comments on your post, create a Comment class:

grails create-domain-class Comment

You want each comment to have a Post that it relates to, a comment field, and the date it was created, so you can display comments in chronological date order. You also should capture who left the comment, so you will need a Commentator class to hold the details of the commentator:

grails create-domain-class Commentator

The next step in building your domain model is to link these classes so you can handle relationships between them. You do this by telling GORM that:

  • A Comment belongs to a Post.
  • A Post has many Comments.
  • A Comment has one Commentator.
  • A Commentator belongs to a Comment.

When you specify one class as belonging to another class you mean it cannot exist without the class to which it belongs. If a Post is deleted, then all its Comments will also be deleted. However, it is possible to delete a Comment without deleting the Post. To achieve the "belongs to" relationship, add a static property to the Comment class:

static belongsTo = Post

You need to create a bi-directional relationship between a Post and its Comments so you can view the comments for a particular post. To do this, add a static property to the Post class:

static hasMany = [comments:Comment]

This will dynamically create a property of type Set called comments on Post objects at run time. However, you want to guarantee the order of the Comment objects on the Post so you need to define your own property as a SortedSet:

SortedSet comments

To set up the relationship between a Comment and its Commentator you need to add a Commentator property on the Comment. You also need to create a relationship from Commentator back to Comment so that you can have the Commentator saved implicitly when a Comment is created. See Listing 1 for the initial domain classes with their relationships defined.

These domain classes define two conventions: the lastUpdated and dateCreated properties. If a domain object has these properties, the GORM will automatically populate the values when the save method is called.

Working with Your Domain Model
Now that you have the basics of your domain model, it's time to work it. You can perform Create, Read, Update, and Delete (CRUD) operations very simply. Add the test case in Listing 2 to the PostTests class, which you will find in the test/integration folder. This class was generated when you created the Post domain class through the Grails command line tool.

Grails provides a constructor that takes named values when you create the Post object, allowing object construction to become much less verbose than in Java. Grails uses the Groovy Meta Object Protocol to inject dynamic methods into domain objects. The available methods are save, get, and delete:

  • The save method either creates or updates the object, depending on whether the object has already been created or not.
  • The get method will retrieve the object from the database, given the database identifier.
  • The delete method removes the object from the database.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date