【问题标题】:Grails "One to Many" relationship without a table join没有表连接的 Grails“一对多”关系
【发布时间】:2012-05-17 14:20:24
【问题描述】:

我是 Grails 和 GORM 的新手,我尝试实现“一对多”关系。 我尝试了文档中的示例:

class Book {
    String title
}
class Author {
    static hasMany = [books: Book]
    String name 
}

这是生成的表格:

AUTHOR
- Id (PK)
- Name

BOOK
- Id (PK)
- Title

AUTHOR_BOOK
- Author_Books_Id
- Book_Id

我期待的更像是:

AUTHOR
- Id (PK)
- Name

BOOK
- Author_Id (PK)
- Book_Index (PK)
- Title

有没有办法实现这一点(摆脱连接表)?

【问题讨论】:

    标签: grails grails-orm grails-domain-class


    【解决方案1】:

    您应该声明这本书属于作者。使用 belongsTo 可以声明 Book 表中有一个外键,它保持对 Author 的 id 列的引用。像这样:

    class Book {
        String title
        static belongsTo = [author: Author]
    }
    
    class Author {
        static hasMany = [books: Book]
        String name 
    }
    

    【讨论】:

      【解决方案2】:

      反过来做就行了。

      一本书有一个作者。

      class Book {
          String title
          Author author
      }
      
      class Author {
          String name 
      }
      

      【讨论】:

        【解决方案3】:

        课本{ 字符串标题 static belongsTo = [作者:作者] }

        类作者{ 静态 hasMany = [书籍:书籍] 字符串名称 }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-19
          • 2012-06-15
          • 1970-01-01
          • 1970-01-01
          • 2010-11-25
          • 2017-09-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多