【问题标题】:grails gorm - not usual one-to-many association - bug found?grails gorm - 不是通常的一对多关联 - 发现错误?
【发布时间】: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


    【解决方案1】:

    我认为你应该使用 Filter 类中的 mappedBy 属性

    class Filter {
    
        static hasMany = [normal:Result, longerDates:Result, locationReduce1:Result, locationReduce2:Result]
        static mappedyBy = [normal: "normalFilter", longerDates: "longerDatesFilter", locationReduce1: "locationReduce1Filter", locationReduce2: "locationReduce2Filter"]
    }
    
    class Result {
        static belongsTo = [normalFilter: Filter, longerDatesFilter: Filter, locationReduce1: Filter, locationReduce2Filter: Filter]
    }
    

    参考是here

    【讨论】:

    • @Bernhard 如果这是正确答案,您应该将其标记为答案 - 这就是 StackOverflow 的工作原理。
    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    相关资源
    最近更新 更多