grails中的多个关联(Multiple associations in grails)

编程入门 行业动态 更新时间:2024-10-24 23:27:14
grails中的多个关联(Multiple associations in grails)

我有一个带有域名餐厅和域名人员的grails应用程序。

class Restaurant { String name static belongsTo = [ owner: Person ] } class Person { String name static hasMany = [ favoriteRestaurants : Restaurant ] }

我的问题是GORM只创建了两个表,餐厅和人,餐厅有owner_id。 然而,我所缺少的是连接表,将一个人最喜欢的餐馆与他联系起来。

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

此外,我最初考虑以下域名:

class Restaurant { String name static belongsTo = [ owner: Person ] static hasMany = [ outstandingCouponOwners : Person ] } class Person { String name static hasMany = [ favoriteRestaurants : Restaurant ] }

哪里有另一个一对多的关系(再一次没有把它映射到另一端)

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 ] }

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.

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)

最满意答案

我想,你必须使用域类的'mappedBy'静态映射。 有关详细信息,请参阅grails参考指南的5.2.1.2部分的底部。 可能有必要在Person的hasMany中引入其他条目:该人拥有的餐馆列表。 尝试以下(完全未经测试的)代码:

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' ] }

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' ] }

更多推荐

本文发布于:2023-08-07 19:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   grails   associations   Multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!