开发者

In Grails, How can I create a domain model to link two of another model?

开发者 https://www.devze.com 2022-12-27 13:57 出处:网络
I\'m currently trying to create a Friendship domain object to link two User objects (with a bit of additional data: createDate, confirmedStatus). My domain model looks as follows

I'm currently trying to create a Friendship domain object to link two User objects (with a bit of additional data: createDate, confirmedStatus). My domain model looks as follows

class Friendship {

User userOne
User userTwo
Boolean confirmed
Date createDate
Date lastModifiedDate

static belongsTo = [userOne:User , userTwo:User]

static constraints = {
userOne()
userTwo()
confirmed()
createDate()
lastModifiedDate()
}
}

I've also added the following entries to the user class

static hasMany = [ friendships:Friendship ]
static mappedBy = [ friendships:'userOne' , friendships:'userTwo' ]

When I do this, the result is a new friendship created (and viewable through the controller) with both users listed in their respective places. When I view the details of userOne, I see the friedship listed. When I view the detail开发者_StackOverflow社区s of userTwo, no friendship is listed. This is not the behavior I expected. What am I doing incorrectly? Why can't I see the friendship listed under both users?


You've declared the userOne and userTwo properties twice in the Friendship class. Once here:

static belongsTo = [userOne:User , userTwo:User]

And again here:

User userOne
User userTwo

Change your Friendship class to this

class Friendship {

  Boolean confirmed
  Date createDate
  Date lastModifiedDate

  static belongsTo = [userOne:User , userTwo:User]

  static constraints = {
    userOne()
    userTwo()
    confirmed()
    createDate()
    lastModifiedDate()
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消