20130515-Grails In Action-3、建模(03小节)
来源:互联网 发布:python培训视频 编辑:程序博客网 时间:2024/05/16 11:46
上一节了解了怎样对对象的属性进行有效性验证以及验证的测试方法,这一节主要了解对象之间的关系
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 email10 String timezone11 String country12 String jabberAddress13 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 profile11 12 static constraints = {13 password(size: 6..8, validator: { passwd, user ->14 passwd != user.userId15 })16 dateCreated()17 profile(nullable: true)18 }19 }
将homepage属性移到Profile对象中,将Profile对象作为一个Profile属性持有,并且允许为空。
为了保证User对象和Profile对象能同步,马上刷新数据,在User对象中再增加一个映射块,将默认的懒加载修改成马上更新
1 ......2 static mapping = {3 profile lazy:false4 }
- 20130515-Grails In Action-3、建模(03小节)
- 20130515-Grails In Action-3、建模(01小节)
- 20130515-Grails In Action-3、建模(02小节)
- 20130515-Grails In Action-3、建模(04小节)
- 20130515-Grails In Action-3、建模(05小节)
- 20130516-Grails In Action-3、建模(06小节)
- 20130516-Grails In Action-4、让模型工作(03小节)
- 20130516-Grails In Action-4、让模型工作(01小节)
- 20130516-Grails In Action-4、让模型工作(02小节)
- 20130516-Grails In Action-4、让模型工作(04小节)
- 20130516-Grails In Action-4、让模型工作(05小节)
- 20130517-Grails In Action-4、让模型工作(06小节)
- Grails in Action:完成啦
- 《Spring Boot in Action》【6. Grails】
- grails obtain controller action
- ActiveMQ in Action(3)
- ActiveMQ in Action(3)
- ActiveMQ in Action(3)
- 20130426-常用站点收藏
- 20130502-Linux试玩随笔-05-Github
- 20130515-Grails In Action-3、建模(01小节)
- win32 sdk inputbox 输入框
- 20130515-Grails In Action-3、建模(02小节)
- 20130515-Grails In Action-3、建模(03小节)
- 20130515-Grails In Action-3、建模(04小节)
- 20130515-Grails In Action-3、建模(05小节)
- 20130516-Grails In Action-3、建模(06小节)
- dp之一维的最大字段和算法
- 20130516-Grails In Action-4、让模型工作(01小节)
- 20130516-Grails In Action-4、让模型工作(02小节)
- 20130516-Grails In Action-4、让模型工作(03小节)
- 20130516-Grails In Action-4、让模型工作(04小节)