例子中用Face 和 Nose作关系的例子
一对一有3种情况:
1.Nose作为Face的一个属性保存. 也就是说Nose 相当于 Face的一个内部类.
2.双向关联
3.级联关联
如果在Nose中设置的是
static belongsTo = Face
static belongsTo = [face:Face]
的区别是,前者是单向关联+级联,face可以通过
face.nose
获得Nose,而
nose.face
是没有的,后者有
原文:
引用
One-to-one
A one-to-one relationship is the simplest kind, and is defined trivially using a property of the type of another domain class. Consider this example:
Example A
class Face {
Nose nose
}
class Nose {
}
In this case we have unidirectional one-to-one relationship from Face to Nose. To make this relationship bidirectional define the other side as follows:
Example B
class Face {
Nose nose
}
class Nose {
Face face
}
This is bidirectional relationship. However, in this case no updates are cascading from either side of the relationship.
Consider this variation:
Example C
class Face {
Nose nose
}
class Nose {
static belongsTo = [face:Face]
}
In this case we use the belongsTo setting to say that Nose "belongs to" Face. The result of this is that we can create a Face and save it and the database updates/inserts will be cascaded down to Nose:
new Face(nose:new Nose()).save()
The example above will save both face and nose. Note that the inverse is not true and will result in an error due to a transient Face:
new Nose(face:new Face()).save() // will cause an error
Another important implication of belongsTo is that if you delete a Face instance the Nose will be deleted too:
def f = Face.get(1)
f.delete() // both Face and Nose deleted
Without belongsTo deletes would not be cascading and you would get a foreign key constraint error unless you explicitly deleted the Nose:
// error here without belongsTo
def f = Face.get(1)
f.delete()
// no error as we explicitly delete both def f = Face.get(1) f.nose.delete() f.delete()
You could keep the previous relationship as unidirectional and allow saves/updates to cascade down by doing the following:
class Face {
Nose nose
}
class Nose {
static belongsTo = Face
}
Note in this case because we are not using the map syntax in the belongsTo declaration and explicitly naming the association. Grails will assume it is unidirectional. The diagram below summarizes the 3 examples:
分享到:
相关推荐
开源项目-jinzhu-gorm.zip,Even easier to handle relationships, try out gorm's Association Mode
gorm文档_英文.pdf
go web项目框架(viper+gin+gorm)适合初学者使用
首先说明的是,在项目中使用orm的好处很多: 防止直接拼接sql语句引入sql注入漏洞 方便对modle进行统一管理 专注业务,加速开发 坏处也是显而易见的: 开发者与最终的sql语句隔了一层orm,因此可能会...会返回gorm.R
简单介绍grails的查询操作,已经类似与hibnatate的查询方式
gen - 将数据库转换为gorm结构体和RESTful api
GORM 剖析与最佳实践(gocn.vip).pdf
参照 https://github.com/jasperxu/gorm-cn-doc.git 生成的pdf文档。
GORM_ADO数据库操作模块.rar
Objective-c 的windows下开发工具。之前你还得装core和sys
易语言GORM_ADO数据库操作模块源码,GORM_ADO数据库操作模块,取易对象错误,刷新属性集,取属性数,取属性信息,取属性信息_v2,置属性信息,置属性信息_v2,取错误数,刷新错误集,清空错误集,取错误信息,是否连接持久文件,...
Gorm 批量插入/向上插入 Gorm Bulk Insert是一个使用实现批量插入/更新Gorm Bulk Insert的库。 目的 保存批量记录 安装 $ go get github.com/sunary/gorm-bulk-insert 这个库依赖于 gorm,除非你已经安装了 gorm,...
NULL 博文链接:https://wushexu.iteye.com/blog/1603657
gcache是gorm的中间件,插入后gorm即刻拥有缓存。 Overview 即插即用 旁路缓存 数据源使用 Redis 防击穿 防穿透 安装 $ go get github.com/8treenet/gcache 快速使用 import ( "github.com/8treenet/gcache" ...
GORM PostgreSQL驱动程序GORM PostgreSQL驱动程序用法import(“ gorm.io/driver/postgres”“ gorm.io/gorm”)// https://github.com/lib/pq dsn:=“ user = gorm password = gorm DB .name = gorm port = 9920 ...
gorm框架操作1
golang 官方的那个操作mysql的有点麻烦所以就使用了gorm,下面就gorm的使用做下简单介绍,感兴趣的朋友跟随小编一起看看吧
GORM - Golang神奇的ORM库,目标是开发人员友好