Friday, March 14, 2008

How to create the Domain-class of Grails--Sparrow begin

After several days basic setup and config,our architecture of project Sparrow has been create.Now we'll start our first step:create domain object.

But before the beginning of our process,let me introduce the requirement of our Sparrow.For facilitate,I will illustrate the requirement by a UML diagram.

As you can see, our project will include three functions.
1)create a project.
2)create issues for this project.
3)create issues type for existing issues.

Following is the Domain Class Model :

Now,let's begin.In Grails,it use the Hibernate for ROM ,but as the same time Grails extend the Hibernate,we call this extension "GOM".It's also based on "Convention Role",so we only need create a POJO class by Groovy language.In our project there are three domain class we'll create.Grail provide us a easy to used tool.To do this,just open the command line.type:

>>grails create-domain-class

then enter the class name ,for example :project.
Grails will auto generate a class with capitalize letter.like this:

class Project {
static hasMany=[issues:Issue]//one project can has many issues
String title
String description
Date createDate
}

Note:the hasMany is a key word of Grails,that means one project can has many issues.so Issue class will have a belongsTo attribute.like this:

class Issue {
static belongsTo=Project
Project project
String summary
String description
Date createDate
}

Because of we have create our database(postgreSql).now we run our application.

>>grails run-app

You'll see that grails auto create the database table for these two domain class.
As you can see Grails auto create two field "id" and "version".it's a convention,also id is a default key and it's request.And also,may be you have saw that the property field "createDate" in database table is create_date,because the createDate field has a capitalize letter "D",so it replace by "_d".yes it's also a convention.

No comments: