Grails Days 1: GORM-CRUD

A basic calculator
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

Reblog this post [with Zemanta]
Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • LinkedIn
  • Slashdot
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • email
  • Twitter
  • FriendFeed

Related posts:

  1. Grails, First Impressions
  2. hibernate tip: smart id generator
  3. Is Grails too much Groovy ?
  4. Grails 101:GroovyBlogs.org
  5. who needs implementations?

  1. No comments yet.

  1. No trackbacks yet.