简体   繁体   中英

Grails GORM many-to-many relationship

I want to delete all the Tweet s when its parent User is deleted, also delete the collaborators when its parent Tweet is deleted. Wherein collaborators if of type User

Currently, Tweet view doesn't hook up collaborators. I am thinking if I'm doing the following right:

Tweet.groovy

User owner
static hasMany = [ collaborators : User ]
static belongsTo = User

User.groovy

static hasMany = [ tweet : Tweet ] 

It seems to me that you lack one relationship that states that a tweet is owned by a particular user. As it is now you will only get a many-to-many relation and I guess you don't want to delete all tweets that a user has collaborated in when you delete a user.

Tweet.groovy

static hasMany = [ collaborators : User ]
static belongsTo = User
User createdBy
static mappedBy = [collaborators : 'tweet']

User.groovy

static hasMany = [ tweet : Tweet ]
static mappedBy = [tweet : 'collaborators']

The mappedBy above is needed since you will have two relationships between the classes and Grails needs to know which one to use for the many-to-many.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM