[英]Grails many to many relationship not creating expected tables
Hi I have 2 domain classes. 嗨,我有2个领域类。 The idea is that a User can create events, as well as be a guest at other peoples events:
这个想法是,用户可以创建事件,也可以作为其他人的事件的客人:
class Event {
Appuser creator
static belongsTo = Appuser
static hasMany = [guests: Appuser]
and 和
class Appuser {
static hasMany = [friends: Appuser, events: Event]
The problem is, I expected it to make a creator_id
column in the event
table, and then an appuser_events
table with just the appuser_id
and the event_id
, but it is also including a creator_id
column here and making it the primary key. 问题是,我希望它在
event
表中创建一个creator_id
列,然后在其中仅包含appuser_id
和event_id
一个appuser_events
表中appuser_id
,但它还在此处包括creator_id
列,并将其作为主键。
I also tried creating my event class like this: 我还尝试过创建这样的事件类:
class Event {
static belongsTo = [creator:Appuser]
static hasMany = [guests: Appuser]
but then grails does not recognize the belongTo relationship. 但是grails无法识别归属关系。
Any idea how to fix? 任何想法如何解决?
Ok, I solved my issue. 好的,我解决了我的问题。 The key was to add 2 hasMany references to the Event class from Appuser, and then add a mappedBy, so my Event Class now contains:
关键是要从Appuser添加2个hasMany对Event类的引用,然后添加一个mappingBy,因此我的Event类现在包含:
static hasMany = [friends: Appuser, events: Event, invites:Event]
static mappedBy = [invites: "guests", events: "creator"]
The Appuser class is unchanged from my first example in the question. 与问题中的第一个示例相比,Appuser类没有变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.