【问题标题】:Grails Many-to-Many BidirectionalGrails 多对多双向
【发布时间】:2014-02-16 02:44:27
【问题描述】:

我在 GORM 中创建多对多双向关系时遇到问题,我找到的解决方案并不是我真正想做的。

我目前建立的关系允许一个作者拥有多本书,但反之则不行(所有权在作者一方)。这是我目前拥有的代码。

class Author {

    String name

    static hasMany = [books:Book]

        static constraints = {
           name(nullable:false)
        }
        String toString() {
           name
        }
}

class Book {

   String name
   String type
   Integer year
   Author authors

   static belongsTo = [authors:Author]
   static hasMany = [authors:Author]

   static constraints = {
       name(nullable:false)
       type(nullable:false)
       year(nullable:true)
       authors(nullable:false)
   }

   String toString() {
      name
   }
}

我希望这种关系是这样的,当我编辑一本书时,我可以选择多个作者,除了在我编辑一个作者时有同一作者的多本书。

【问题讨论】:

    标签: grails many-to-many grails-orm bidirectional


    【解决方案1】:

    Grails 支持多对多的关系,你的代码必须能正常工作...但是有一个小问题...脚手架不支持它,所以你必须编写自己的代码来处理你想要的关系!

    【讨论】:

    • 啊哈,这很有意义。谢谢毛里!
    猜你喜欢
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2012-10-11
    • 1970-01-01
    • 2019-07-29
    相关资源
    最近更新 更多