Grails:两个域对象之间的多重关系 [英] Grails: multiple relationships between two domain objects

查看:104
本文介绍了Grails:两个域对象之间的多重关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Grails中实现两个不同类型的关系。



  class作者{
字符串名称
}

类书籍{
字符串标题
static belongsTo = [作者:作者]

}

以上描述了作者和书籍之间非常基本的一对多关系。
但是我也希望作者有一个最喜爱书籍列表的概念。理想情况下,这将表示为一个单独的一对多关系,将相同的Book对象描述为一个列表并保持这样。

  class作者{
字符串名称
静态hasMany = [收藏图书:图书]

静态映射= {
收藏图书joinTable:[name:'favourite_books',
key:'author_id']
}
}

class Book {
String title
static belongsTo = [client:Client]

}

我试图用上面的描述来描述它(在许多其他方法中)并最终数据库表(favourite_books)未创建。我没有得到任何错误。这是我可以考虑的唯一方法,不需要使用任何我不想让模型保持简单的额外对象。
我觉得我在正确的轨道上,但也许错过了一些重要的难题。



任何帮助都将不胜感激。

解决方案

最后解决了这个问题。
感谢Don将我指向了db-reverse-engineer插件的方向,这个插件帮助公开了允许这种映射策略的关键属性。
基本上,这一切都归结为使用GORM的mappedBy关联设置来显式地告诉Grails多个has有多少个引用应该被映射。
工作的类文件如下所示:

  class作者{

字符串名称
static hasMany = [books:Book,favorites:Book]

//需要用
//'mappedBy'属性消除多重hasMany引用:
static mappedBy = [books:author,
favorites:authors]
}

class Book {

String title
作者作者

静态hasMany = [作者:作者]
静态belongsTo = [作者]
}

再次感谢您的帮助


I am trying to implement two different types of relationships between two domain classes in Grails.

Consider the following; I have two domain classes, an Author and Book class with an Author having many Books.

class Author{           
   String name 
}

class Book{
   String title
   static belongsTo = [author:Author]

}

The above depicts a very basic one to many relationship between an Author and a Book. But I also want an Author to have a concept of a list of favourite books. This would ideally be represented as a separate one to many relationship describing the same Book object as a list and persisted as such.

class Author{          
   String name
   static hasMany = [favouriteBooks: Book]

   static mapping = {
        favouriteBooks joinTable: [name: 'favourite_books',
                key: 'author_id']
   }
}

class Book{
   String title
   static belongsTo = [client:Client]

}

I have tried to describe this as above (among many other methods) and ultimately the database table (favourite_books) is not created. I do not get any errors. This is the only way I can think of doing this without using any extra objects which I would like to avoid to keep the model simple. I feel I'm on the right track but maybe missing some vital piece of the puzzle.

Any help would be much appreciated.

解决方案

Finally figured this out. Thanks to Don for pointing me in the direction of the db-reverse-engineer plugin which helped expose the key property that allows for this mapping strategy. Basically it all came down to using GORM's mappedBy association setting to explicitly tell Grails how the multiple hasMany references should be mapped. The class files that worked are as follows:

class Author {

    String name
    static hasMany = [books: Book, favourites: Book]

    // need to disambiguate the multiple hasMany references with the 
    // 'mappedBy' property:
    static mappedBy =   [books: "author",
                        favourites: "authors"]
}

class Book {

    String title
    Author author

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

Thanks again for the help

这篇关于Grails:两个域对象之间的多重关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆