上一节了解了怎样对对象的属性进行有效性验证以及验证的测试方法,这一节主要了解对象之间的关系
1:1关系
User对象主要用于登录验证操作,User对象还有一些比较杂的属性,我们单独用一个Profile对象保存,这样User和Profile之间就是1:1的关系
1、增加一个Profile对象
grails create-domain-class com.grailsinaction.Profile
2、给Profile对象增加一些用户属性
1 package com.grailsinaction
2
3 class Profile {
4 static belongsTo = User
5 byte[] photo
6 String fullName
7 String bio
8 String homepage
9 String email
10 String timezone
11 String country
12 String jabberAddress
13
14 static constraints = {
15 fullName(nullable: true)
16 bio(nullable: true, maxSize: 1000)
17 homepage(url: true, nullable: true)
18 email(email: true, nullable: true)
19 photo(nullable: true)
20 country(nullable: true)
21 timezone(nullable: true)
22 jabberAddress(email: true, nullable: true)
23 }
24 }
这里最明显的是static belongsTo = User这一句,(直接翻译过来就是“属于”),告诉系统对象之间怎么级联,这个级联的意思是,只要User对象删除了,和这个User相关联的Profile对象也级联删除。belongsTo在n:m关系中还有其他用途
3、修改User对象的属性
1 package com.grailsinaction
2
3 class User {
4
5 String userId
6 String password
7
8 Date dateCreated
9
10 Profile profile
11
12 static constraints = {
13 password(size: 6..8, validator: { passwd, user ->
14 passwd != user.userId
15 })
16 dateCreated()
17 profile(nullable: true)
18 }
19 }
将homepage属性移到Profile对象中,将Profile对象作为一个Profile属性持有,并且允许为空。
为了保证User对象和Profile对象能同步,马上刷新数据,在User对象中再增加一个映射块,将默认的懒加载修改成马上更新
1 ......
2 static mapping = {
3 profile lazy:false
4 }
分享到:
相关推荐
groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part1 共两个压缩包,解压后将扩展名.zip.bak改为.zip再次解压。
elasticsearch-grails-plugin, 恢复的ElasticSearch grails插件 Elasticsearch插件插件这个项目是一个基于Elasticsearch的插件,这个项目是基于的人完成的伟大工作的。你为什么想要为原来的Elasticsearch插件提供...
groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part2 共两个包,解压后需要将扩展名.zip.bak改名为.zip重新解压。 http://dist.springsource.com/release/STS/3.8.1.RELEASE/dist/ e4.6/spring-tool-...
Getting-Started-with-Grails-Chinese,一本不错的grails入门书籍,希望对有志于grails的朋友们有用。
Grails In Action.pdf ebook
Grails in Action pdf
Grails in Action - Presentation pdf
Grails in Action Grails in Action Grails in Action Grails in Action Groovy in Action Grails in Action Grails in Action Groovy in Action Groovy in Action
wsdl2java源码 请注意,当前的 3.x 版本使用 cxf 3.0.4 并且需要 WSS4J ...grails-app\endpoints** 和 grails-app\services** 目录中自动装配配置的类 端点创建脚本 create-endpoint 和 create-endpoin
ant-1.9.4jarant-antlr-1.9.4.jarant-junit-1.9.4jar ant-launcher-1.9.4.jar bsf-2.4.0.jar commons-cli-1.2jar commons-logging-1.2.jar gpars-1.2.1.jar groovy-2.4.5jar groovy-ant-2.4.5.jar ...
基于java的动态语言Groovy 构建的敏捷web开发框架grails经典入门教程
Grails In Action 2009最新版,英文的。很详细,学习groovy和grails的不二之选。
Grails入门指南中文pdf -- 针对grails1.0.4更新,附加idea8 开发grails的流程
实测可用
实测可用
实测可用
实测可用