通过belongsTo保存引用

发布于 2024-12-05 04:56:29 字数 429 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喵星人汪星人 2024-12-12 04:56:29

我不知道 GORM 在这种情况下会如何表现,因为你实际上已经进入了这个奇怪的区域,在该区域中,Person 上有一个单向 hasMany ,这会导致 SAVE-UPDATE 级联Person 的行为和 Address 上的 NONE 。然后,Person 和 OAP 之间也有一个单向的一对一,这会导致 OAP 端出现 ALL 级联行为,而在 Address 端出现 NONE 。所以我不确定在这里会发生什么。您需要将关系修复为:

  • 使其成为 OAP 而不是 Person hasMany = [address: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 a SAVE-UPDATE cascade behavior from Person and a NONE on Address. Then you also have a unidirectional one-to-one between Person and OAP which results in an ALL cascade behavior on the OAP side, and a NONE on the Address side. So I'm not sure what to even expect here. You need to fix the relationship to either:

  • Make it so OAP and not Person hasMany = [address:Address]
  • Make it so 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.

森末i 2024-12-12 04:56:29

请尝试一下,它解决了我的问题

p.addToAddresses(a);
p.save(flush:true)

Please try with this, it resolved my problem

p.addToAddresses(a);
p.save(flush:true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文