gpt4 book ai didi

hibernate - Grails GORM 多对多关系和可连接条目的删除

转载 作者:行者123 更新时间:2023-12-01 04:04:39 25 4
gpt4 key购买 nike

我有 2 类汽车和车库。他们有一个 m:n 关系,所以一辆车有很多车库,一个车库有很多车。

我的数据库中有一个garage_has_car 表,其中存储了关系。
我已经在车上配置了“belongsTo”到“车库”

static belongsTo = [  Garage ]

如果我清除车库上的汽车集合并保存,garage_has_car 表中的相应条目将被删除。

但是如果我清除汽车上的车库集合并保存,garage_has_car 表中的相应条目不会被删除!

我怎样才能实现这种行为?

更新 1(添加关系定义):

在车库中,关系是:
cars column:'deskriptor_id',joinTable:'garage_has_cars'

在汽车中是
static belongsTo = [  Garage ]
garages column:'car_id',joinTable:'garage_has_cars'

我也在 IRC 里问过这个问题,有人回答:

(...) grails many to many relationships are always only cascade one direction. also many-to-many relationships in grails are horribly inefficient with how they get mapped in hibernate. don't use them. create a joinclass domain object CarGarage with 1 car and 1 garage. make cars and garages properties of your classes transients and add get and add methods to the car and garage classes which use your joinclass for maintaining the relationship. you also want to disable versioning in the join class here's some info can be found in this presentation http://burtbeckwith.com/blog/files/169/gorm%20grails%20meetup%20presentation.pdf and the notes of it are here: Grails: Many-to-Many without hasMany/belongsTo - instead using native 3NF - Searching full text



我发布这句话是因为它可以帮助与我有同样问题的人。一旦我有时间实现这些更改,我也会更新这个问题。

最佳答案

只需回答即可从未回答列表中删除。

(...) grails many to many relationships are always only cascade one direction. also many-to-many relationships in grails are horribly inefficient with how they get mapped in hibernate. don't use them. create a joinclass domain object CarGarage with 1 car and 1 garage. make cars and garages properties of your classes transients and add get and add methods to the car and garage classes which use your joinclass for maintaining the relationship. you also want to disable versioning in the join class here's some info can be found in this presentation http://burtbeckwith.com/blog/files/169/gorm%20grails%20meetup%20presentation.pdf and the notes of it are here: Grails: Many-to-Many without hasMany/belongsTo - instead using native 3NF - Searching full text

关于hibernate - Grails GORM 多对多关系和可连接条目的删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10261203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com