Grails Days 1: GORM-CRUD

- Image via Wikipedia
It has been more than a week since I downloaded Grails, Although my first Grails impressions are not so well, I am playing with Grails with lots of curiosity and joy,
first thing you should learn about grails is in fact Groovy, but I will skip this part
first setup all enviroment and IDE then create new blog Project
mkdir groovypress
cd groovypress
grails create-app groovypress
GORM
GORM is Groovy Object Relation Mapping as you can easily predict, but the point is grails has no special ORM engine but hibernate !!! so GORM is nothing but a special groovy-hibernate notation,
for a new Persistant domain object use the command below
grails create-domain-class Blog
paste the code below
class Blog {
static hasMany = [blogPosts:BlogPost,blogTags:BlogTag,blogVote:BlogVote]
String title
String body
String url
int status
}
As you can see nothing so familiar but still you can read and guess, and let the grails wizards make it even faster
grails generate-all Blog
now you can see and read both controller and view gsp on a single CRUD operation
CRUD
here comes the real tricky part, unlike pure java POJOs groovy objects are not only POJOs but also DAOs :
def update = {
def blogInstance = Blog.get( params.id )
if(blogInstance) {
blogInstance.properties = params
if(!blogInstance.hasErrors() && blogInstance.save()) {
flash.message = "Blog ${params.id} updated"
redirect(action:show,id:blogInstance.id)
}
else {
render(view:'edit',model:[blogInstance:blogInstance])
}
}
else {
flash.message = "Blog not found with id ${params.id}"
redirect(action:edit,id:params.id)
}
}
here you can see all read save and update methods, ultimate POJOs,
and end of the day
PS: I use Netbeans with pleasure for grails development, it has lots of short cuts and console support and works really amazing
Related posts:
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=7e4c4c8c-0acc-4d1b-9fc0-20c56ae68014)



No comments yet.