通过belongsTo保存引用
我正在使用 grails 中一些已经创建的(不是我创建的)域类来制作脚本。
class Person extends OAP {
static hasMany = [addresses: Address]
(...)
}
class Address {
static belongsTo = [oap: OAP]
(...)
}
OAP 类没有对地址的引用。
所以我试图这样做:
p.save()
a.oap = p
println a.oap
a.save()
p是Person,a是Address,但是虽然它在控制台上打印了正确的人,但引用没有保存在地址表上(oap_id保持为空)
PS:这可能不是最好的关系在 grails 中设置,但这就是我必须处理的
I'm making a script using some already created (not by me) domain classes from grails.
class Person extends OAP {
static hasMany = [addresses: Address]
(...)
}
class Address {
static belongsTo = [oap: OAP]
(...)
}
class OAP has no reference to Address.
So I was trying to do:
p.save()
a.oap = p
println a.oap
a.save()
with p being Person and a being Address, but although it prints the correct person on the console, the reference is not saved on the address table (oap_id stays null)
P.S.: It may not be the best relationship set-up in grails, but that's what I have to work with
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
p.addToAddresses(a)
,然后p.save()
。它应该同时保存p
和a
。请参阅http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html#5.2.4%20Sets、%20Lists%20and%20Maps< /a>
Try
p.addToAddresses(a)
and thenp.save()
. It should save bothp
anda
.See http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html#5.2.4%20Sets,%20Lists%20and%20Maps
我不知道 GORM 在这种情况下会如何表现,因为你实际上已经进入了这个奇怪的区域,在该区域中,Person 上有一个单向
hasMany
,这会导致SAVE-UPDATE
级联Person 的行为和 Address 上的NONE
。然后,Person 和 OAP 之间也有一个单向的一对一,这会导致 OAP 端出现ALL
级联行为,而在 Address 端出现NONE
。所以我不确定在这里会发生什么。您需要将关系修复为:hasMany = [address:Address]
belongsTo = [person:Person]
或者,就你想在你们的关系中做什么做一些额外的解释,我们就可以从那里开始。
I have no idea how GORM will behave in this situation because you have essentially entered into this weird zone where you have a unidirectional
hasMany
on Person which results in aSAVE-UPDATE
cascade behavior from Person and aNONE
on Address. Then you also have a unidirectional one-to-one between Person and OAP which results in anALL
cascade behavior on the OAP side, and aNONE
on the Address side. So I'm not sure what to even expect here. You need to fix the relationship to either:hasMany = [address:Address]
belongsTo = [person:Person]
Or, give some additional explanation as to what you're trying to do in your relationship and we can go from there.
请尝试一下,它解决了我的问题
Please try with this, it resolved my problem