Association in GORM

理解级联更新和删除
有三个需要理解:"belongsTo"设置,谁是控制,谁是所有者
任何一种关系,只要你设置了belongsTo,"update"和"delete"将会从拥有者到被拥有着产生级联。
否着,你就有手动控制这些.

class A { static hasMany = [bees:B] }
class B { static belongsTo = [a:A] }
the cascade strategy is set to "ALL" for the one side and "NONE" for the many side

class A { static hasMany = [bees:B] }
class B { }
In the case of a unidirectional one-to-many where the many side defines no belongsTo then the cascade strategy is set to "SAVE-UPDATE"

class A { static hasMany = [bees:B] }
class B { A a }
the cascade strategy is set to "SAVE-UPDATE" for the one side and "NONE" for the many side

class A { }
class B { static belongsTo = [a:A] }
In the case of a unidirectional one-to-one association that defines a belongsTo then the cascade strategy is set to "ALL" for the owning side of the relationship (A->B) and "NONE" from the side that defines the belongsTo (B->A)

第一节.One-One
有三种关联描述
A.Face----->Nose
B.Face<---->Nose
C.Face<-----Nose
------->
belongsTo
代码实现
A.
class Face {
Nose nose
}
class Nose {
}
关系:单向连接。
B.
class Face {
Nose nose
}
class Nose {
Face face
}
关系:双向连接。
操作:任何一边操作Update不会影响到两个
C.
class Face {
Nose nose
}
class Nose {
static belongsTo = [face:Face]
}
关系:使用belongsTo的双向关联,控制权在Face上。
操作:更新,添加,删除Face操作将会影响到Nose
问题:Nose这些操作结果是什么了?

建议:采用单向连接,并使用belongsTo明确关系。

第二节:one-to-many
代码:
class Author {
static hasMany = [ books : Book ] String name
}
class Book {
String title
}
关系:单向一对多 table方式:连接表,或者可以用ORM DSL使用外键连接
抓取机制“lazy”延迟加载,问题,会产生n+1问题
def a = Author.get(1)a.books.each {
println it.title
}
代码
class Author {
static hasMany = [ books : Book ] String name
}
class Book {
static belongsTo = [author:Author]
String title
}
默认的级联只会有Update,Save方法,
除非加入belongsTo
static mappedBy = [flights:"departureAirport"]

第三节 many to many
代码
class Book {
static belongsTo = Author
static hasMany = [authors:Author]
String title
}
class Author {
static hasMany = [books:Book]
String name
}
控制权在Author,author有责任持久这种关系,而且,只有author能够产生级联across。
new Book(name:"Groovy in Action")
.addToAuthors(new Author(name:"Dierk Koenig"))
.addToAuthors(new Author(name:"Guillaume Laforge"))
.save()
注意这段代码是只会添加Book.
建议:和hibernate的级联机制一样。

组合compositon
class Person {
Address homeAddress
Address workAddress
static embedded = ['homeAddress', 'workAddress']
}
class Address {
String number
String code
}
这里面只会有一张表

继承

class Content {
String author
}
class BlogEntry extends Content {
URL url
}
class Book extends Content {
String ISBN
}
class PodCast extends Content {
byte[] audioStream
}

持久化的基础知识:
有一关键点要记住Grails本质是使用hibernate做持久化。
Grails自动绑定一个hibernate session 给现在执行的请求。
Saving和updating
def p = Person.get(1)
//不用加入事物代码,由Grails自动完成
p.save()

flush方法
def p=Person.get(1)
p.delete(flush:true)
p.save(flush:true)

批处理
Customer.executeUpdate("delete Customer c where c.name = :oldName", [oldName:"Fred"])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值