Wednesday, January 28, 2009

Grails Tips Part I – Domain Classes

I have been working a lot with Grails lately and I have to say that I love it!

It has allowed me to create some great web applications in a very fast and reliable way. While developing these applications, I learned a lot of useful things that I would like to share to you. Today I’ll give you some tips regarding Domain Classes.

1) Date created & Last Updated

If you want Grails to handle the date in which an object is created and the last time it was updated, all you have to do is add the following attributes to your domain class:

Date dateCreated
Date lastUpdated

As easy as that! Grails will automatically set the value for this two attributes when an object is created and every time it is updated.

2) Field order

If you want Grails to generate the fields for the create, edit & show views in a particular order, just define that order on the constraints. If the attribute doesn’t require a specific constraint (such as maxSize, unique, nullable, etc), just put the attribute on the constraints and leave the constraint empty.


static constraints = {
name()
address()
city(nullable:true, blank:true)
phone(nullable:true, blank:true)
mobile(nullable:true, blank:true)
email(email:true)
}


3) Unique by group

If you want an attribute on a domain class to be unique, but only grouped by another attribute, you can do it. In the following example, the company name will be unique only for companies from the same client:


class Company {

Client client
String name
String address

static constraints = {
name(size:3..50, blank:false, unique:'client')
address()
}
}


4) Change database mapping for String attribute

Grails will map the String attributes on your domain class to varchar(255) by default. If you want a String attribute mapped to text, here’s how you do it:


static mapping = {
columns{
attribute_name type:"text"
}
}


Stay tuned for more Grails tips!

43 comments:

Lukasz said...

Very useful tips! I am waiting for more!

tomas said...

Nice, but you also want to add constraints to your lastUpdated and dateCreated so that it doesn't show on the CRUD screens...

static constraints ={
lastUpdated(display:false)
dateCreated(display:false)
}

Azi said...

Nice. I wanted to share some tips which I know

In case field has no maxSize but should still be rendered within textarea then use widget:'textarea' in constraint. e.g:

def constraints = {
description(widget:'textarea')
}
Other widget type includes:
1. textField
2. hiddenField
3. submitButton
4. field
5. textArea
6. form
7. actionSubmit
8. actionSubmitImage
9. datePicker
10. renderNoSelectionOption
11. timeZoneSelect
12. localeSelect
13. currencySelect
14. select
15. checkBox
16. radio

Parag said...

Thanks, found the tips on managing dates very useful.

Learner said...

Very nice tips. Thanks for posting.

Srinath said...

very nice.i want to know one thing.i hope you can help me for grails application.

i have existing JPA entity classes in another spring and hibenrate application.i just want to reuse the entities in grails application without storing into src/java.

if you know how to do it please help me in that

阿凡達 said...

你要保守你的心,勝過保守一切,因為一生的果效是由心發出..................................................

勇氣 said...

完成一個小目標,會把自己推向一個大目標 ....................................................

said...

若對自己誠實,日積月累,就無法對別人不忠了。........................................

麗君 said...

處順境須謹慎,處逆境要忍耐。........................................

少菁 said...

may the blessing be with you.........................................

嘉容嘉容 said...

世間是非,要如水泥地般水過則乾。......................................................

冠樺 said...

恨一個人,比原諒一個人,更傷力氣。 ..................................................

奕玲 said...

徐可寫真yam交友080中部人聊天室免費成人電影聊天室080正妹牆正妹論壇正妹影片成人 影片sex日本愛情影片日本卡通a片下載日本同志男優日本同志色教館 avdvd 日本同志色教館 g8yy日本同志卡通影片交流區日本同志漫畫日本圖片網站日本地圖日本夜店影片視訊 辣妹視訊 辣妹免費影片分享免費聊天66k日本a電影免費18 成人girl5320微風成人情色 網6k聊天館aio 交友愛情館免費上班族聊天室免費性影片666成人光金瓶影片交流區美眉泰正點麗的情色遊戲麗的情色小遊戲亞亞成人館曼雪兒免費情色文學曼雪兒免費情色小說曼雪兒免費色曼雪兒免費色倩小說曼雪兒免費色小說

BokHaile8854 said...

珍惜當下..活在當下..祝大家都平平安安健健康康!........................................

智文 said...

志不立,天下無可成之事。.............................................

麗威麗威 said...

上來打聲招呼,祝你一切平安!.............................................

AmiraE_Ta said...

999無瑪試看圖uu 視訊高雄情人援交 免費成人影片,日本美女寫真集 情色影片 亞洲禁果影城貼片 520sex免費視訊 日本av女優,免費性短片欣賞 aa免費線上85cc電影直播 免費視訊美女-殺很大 聊天室交友 淫美論壇 a383l影音城 一本道 a片 東京熱成人圖片區 情色avdvd貼片 台南援交月光論壇 UT視訊交友85cc影城 85x1x成人影院 gay壞朋友 ut正妹 聊天室 高中生援交 視訊0204movie免費影片 34c影音視訊聊天室 美女交友-免費視訊 制服妹影片 免費av18禁 080 中部人聊天室 上班族俱樂部 utobe 影片分享 sexy girl辣妹妹影音視訊 嘟嘟貼圖區av影片下載 557557視訊聊天 免費線上成人影片bonbonsex s383視訊 找援交小二館遊戲區免費av kk俱樂部 c字庫-視訊美女 免費h影片,成人影片分享 kiss 影音視訊聊天室 520sex成人視訊 本土自拍貼圖區sex hi5 tv免費影片百分百貼圖區 1007視訊聊天 sogo 色論壇,嘟嘟情人色網影片 美女交友-視訊聊天 a漫hilive tv免費電影 豆豆情色視訊aio aaa 片試看 視訊豆豆情色080 qq美美色網影片,彩虹頻道免費影片 kk69視訊俱樂部

竹青 said...

go2av免費影片sex 0401 影音 live 秀視訊聊天交友 色美眉台中援交aa 片試看 a免費視訊聊天網 情人34C波霸美女 香蕉鮑魚俱樂部,免費av 免費線上 aa 片試看 g點無碼a片 性愛姿勢 辣妹sex story 視訊聊天室 sex888免費看影片波霸美女寫真 視訊美女pixnet 成人交友hibb 4u成人視訊 情人080視訊網 私密論壇sex888免費看影片論壇 Show-live視訊聊天室 辣妹貼圖a片天堂 a片免費看 情色視訊禁地論壇 日本 a 片,無碼影片,美女,sexy,a片天堂 小魔女免費影城 視訊美女sogo 色論壇 s38live秀 卡通aa片免費看自拍天堂 洪爺免費線上歐美A片段觀看 亞洲東洋影片 0941 影音視訊聊天室 18成人85cc影城0204movie 成人漫畫區月光論壇 a片-情色成人影片 免費視訊toolbox 707網愛聊天室 1111辣妹貼圖,寫真集辣妹 av無碼月光論壇辣妹視訊 視訊辣妹主播脫衣秀 aio性愛dvd辣妹影片直播 黑澀會美眉 mv 高雄視訊,qq 美女貼圖片區 5i176 免費視訊秀 168論壇視訊辣妹 a片天堂s383視訊 bt電影下載,aa 片 sexy girl video movie 080cc成人色網 後官0951主入口 視訊聊天評比 視訊交友聊天室 no8 sogo論壇視訊辣妹

730A_ngelinaRabideau0 said...

波霸口交18禁貼圖寫真視訊援交露點爆乳潮吹裸體裸照裸女愛愛無碼尋夢視訊聊天a漫a片a圖一夜情一葉情人妻激情情色寫真美女自拍辣妹自拍正妹自拍美女走光辣妹走光正妹走光脫衣秀脫衣走光

Re弘娟neS_Seely0216弘娟 said...

Better say nothing than nothing to the purpose. ........................................

asda said...

量力而為,別勉強了,Cut your coat according to your cloth...................................................

冠慧 said...

Necessity is the mother of invention...................................................................

俊偉 said...

一沙一世界,一花一天堂,掌中握無限,剎那即永恆......................................................................

林書豪 said...

河水永遠是相同的,可是每一剎那又都是新的。....................................................................

萱祥 said...

Poverty is stranger to industry.....................................................................

泓發 said...

成熟,就是有能力適應生活中的模糊。.................................................................

佩怡佩怡佩怡 said...

如果成為一支火柴,也要點亮一個短暫的宇宙;如果是一隻烏鴉,也要叫疼閉塞的耳膜。.................................................................

JasonBirk佳琪 said...

要用心經營哦~~祝福你~~
..................................................

俊成俊成 said...

逛BLOG逛上癮了,一天不來會不習慣了耶!!..................................................................

芸茂芸茂 said...

新手上路哦~請大家多支持^_^..................................................................

蔡舜娟蔡舜娟 said...

來看看你逛逛blog囉,加油!............................................................

彥安 said...

你怎麼能經過一片海,而忘記它的藍?............................................................

吳庭 said...

Man proposes, God disposes..................................................................

建張青建張青 said...

鞋匠能作好鞋子,因為他只做鞋,不做別的。..................................................

允黃淑 said...

Learning makes a good man better and ill man worse.............................................................

亦奈美妮 said...

良言一句三冬暖,惡語傷人六月寒。......................................................................

韋黃宣 said...

愛情是盲目的,但婚姻恢復了它的視力。..................................................................

偉曹琬 said...

Every moment of resistance to temptation is a victory.............................................................

國林林林林維 said...

在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」............................................................

建枫 said...

與人相處不妨多用眼睛說話,多用嘴巴思考,............................................................

佳張張張張燕張張張張張 said...

看到大家都留言-我也忍不住說聲---加油..................................................

張王雅竹欣虹 said...

亞洲大學正妹 正妹寫真 正妹寫真館 夜店正妹照片 正妹實驗室 正妹桌布 正妹無名