grails gorm - 不是通常的一对多关联 - 发现错误了吗?
首先:我不是 grails 的初学者。我多次使用了一对多关联。也许问题的发生是因为它不是通常使用的。
代码:
class Filter {
static hasMany = [normal:Result, longerDates:Result, locationReduce1:Result, locationReduce2:Result]
}
class Result {
int score
static belongsTo = [user:User, filter:Filter]
static constraints = {
user(nullable:false, blank:false)
score(nullable:false)
}
如您所见。我使用“结果”类 4 次作为与同一类“过滤器”的一对多关联,
grails 使用外键“filter_id”创建表“结果”以引用关联的过滤器。但如果它是“正常”/“更长日期”.... 关联,则没有区别。结果当我查询时
def 结果=filter.normal
或
def 结果=filter.longerDates
我得到相同的结果。虽然我保存了正确的结果
filter.addToNormal(new Result(..))
和
filter.addToLongerDates(new Result(..))
我尝试的下一件事是从结果到过滤器删除belongs_to 关联。虽然我想在删除过滤器时隐式删除过滤器的所有结果...
结果是我想要的“结果”表。 4 个属性称为“正常”、“longer_dates”、“location_Reduce_1”和“location_Reduce_2”。当我保存“正常”结果时,表中的属性包含结果所属的过滤器 ID。其他属性(例如“longDates”)具有空值)。 到目前为止,一切都很好。奇怪的是,尽管我在“filter”类中有一个 has_many 属性,但每个过滤器只能保存一个结果,
有人知道我做错了什么吗?
first of all: i am not a beginner in grails. I used a one-to-many association many times. Maybe the problem is happening because it's not a usual use.
The code:
class Filter {
static hasMany = [normal:Result, longerDates:Result, locationReduce1:Result, locationReduce2:Result]
}
class Result {
int score
static belongsTo = [user:User, filter:Filter]
static constraints = {
user(nullable:false, blank:false)
score(nullable:false)
}
as you can see. i am using the Class "result" 4-times as a one to many association to the same class "filter"
grails creates the table "result" with a foreign key "filter_id" to reference to the associated filter. But no difference is made if its the "normal"/ "longerDates" .... association. As a result when i query
def results = filter.normal
or
def results =filter.longerDates
i get the same results. although i saved the results right
filter.addToNormal(new Result(..))
and
filter.addToLongerDates(new Result(..))
the next thing i tried was deleting the belongs_to association from the result to the filter. Although i wanted to implicitly delete all results of a filter when the filter is deleted...
the result was a "result" table as i wanted it. 4 attributes called "normal" "longer_dates", "location_Reduce_1" and "location_Reduce_2". when i saved a "normal" result the attribute in the table contained the filter id to which the result belongs. the other attributes (like longerDates" had the null value).
so far so good. the strange thing that i was only able to save ONE result per filter although i had a has_many attribute in the "filter" class
has anybody any idea what i am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该在 Filter 类中使用mappedBy属性
参考是此处
I think you should use the mappedBy property in Filter class
The reference is here