Grails中的多个关联 [英] Multiple associations in grails

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

问题描述

我有一个带域名Restaurant和域名Person的grails应用程序。

I have a grails app with a domain Restaurant and a domain Person.

class Restaurant {
  String name

  static belongsTo = [ owner: Person ]
}

class Person {
  String name

  static hasMany = [ favoriteRestaurants : Restaurant ]
}

我的问题是GORM只创建两个表,Restaurant和Person,其中Restaurant有owner_id。然而,我所缺少的是连接一个人最喜欢的餐馆的连接表。

My problem is that GORM creates only two tables, Restaurant and Person, where Restaurant has an owner_id. However what I am missing is the join table that links a person's favorite restaurants back to him.

我可以理解GORM为什么这样做(双向一对多),但我无法弄清楚如何以我想要的方式进行操作(1x单向一对多,1x单向多对一)。我想我应该使用 mappedBy ,但我不知道是什么将其映射为没有任何链接返回: - (

I can understand why GORM does it this way (bidirectional one-to-many), however I can't figure out how to do it the way I want (1x unidirection one-to-many, 1x unidirectional many-to-one). I guess I should use mappedBy but I do not know what to map it to as there is nothing linking it back :-(

另外,我最初考虑了以下域:

Additionally, I was initially considering the following domains:

class Restaurant {
  String name

  static belongsTo = [ owner: Person ]
  static hasMany = [ outstandingCouponOwners : Person ]
}

class Person {
  String name

  static hasMany = [ favoriteRestaurants : Restaurant ]
}

哪里有另一个一对多的关系(并且没有任何关系可以将它映射到另一端)

where there is another one-to-many relationship (and again with nothing to map it to on the other end)

推荐答案

我认为,您必须使用domain类的'mappedBy'静态映射。有关详细信息,请查看 grails参考指南的第5.2.1.2节。
可能需要在Person的hasMany中添加更多条目:该人拥有的餐馆列表。尝试以下(完全未经测试)代码:

I think, you have to use the 'mappedBy' static map of a domain class. For details look at the bottom of section 5.2.1.2 of the grails reference guide. It might be necessary to introduce additional entries in Person's hasMany: the list of restaurants owned by the person. Try the following (completely untested) code:

class Restaurant {
  String name

  static belongsTo = [ owner: Person ]
  static hasMany = [ outstandingCouponOwners : Person ]
}

class Person {
  String name

  static hasMany = [ favoriteRestaurants : Restaurant, owns: Restaurant, coupons: Restaurant ]
  static mappedby = [ owns: 'owner', coupons: 'outstandingCouponOwners' ]
}

这篇关于Grails中的多个关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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