Grails多对多双向

时间:2014-02-16 02:44:28

标签: grails many-to-many gorm bidirectional

我在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
   }
}

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

1 个答案:

答案 0 :(得分:0)

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

相关问题