【发布时间】:2011-11-07 14:37:08
【问题描述】:
首先:我不是 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”创建表“result”以引用关联的过滤器。但是,如果它是“正常”/“longerDates” .... 关联,则没有区别。结果当我查询时
def 结果 = filter.normal
或
def 结果 =filter.longerDates
我得到相同的结果。虽然我保存了正确的结果
filter.addToNormal(new Result(..))
和
filter.addToLongerDates(新结果(..))
我尝试的下一件事是从结果中删除 belongs_to 关联到过滤器。虽然我想在删除过滤器时隐式删除过滤器的所有结果...
结果是我想要的“结果”表。 4 个属性称为“正常”“longer_dates”、“location_Reduce_1”和“location_Reduce_2”。当我保存“正常”结果时,表中的属性包含结果所属的过滤器 ID。其他属性(如 longDates 具有空值)。 到目前为止,一切都很好。奇怪的是,尽管我在“过滤器”类中有一个 has_many 属性,但每个过滤器只能保存一个结果
有人知道我做错了什么吗?
【问题讨论】:
标签: grails grails-orm