Grails中多对多和一对多冲突问题

5

我有以下两个领域类, 用户和帖子 并且它们之间有两个关系, 用户与帖子具有反向引用的1对多关系。 用户与他关注的帖子之间具有多对多关系: 我的关系如下:

User {
hasMany = [posts : Post, followingPosts: Post]
belongsTo = [Post] //For the many-to-many, this is the owner i'd like to have.

}

Post {
  hasMany = [followers: User]
  belongsTo = [owner: User] //For the 1-to-Many, this is my back-reference
}

现在我在Grails中遇到了冲突,我尝试通过映射来解决它,但是没有成功,这是我得到的错误:

    Domain classes [Post] and [User] cannot own each other in a many-to-many relationship. Both   contain belongsTo definitions that reference each other. (Use --stacktrace to see the full trace)

有人知道如何解决这个问题吗?
1个回答

1

我认为你可以使用mappedBy来完成,例如:

class User{

  static hasMany = [posts : Post, followingPosts: Post]
  static mappedBy = [posts : "user"]
}


class Post{  

  User user
  static hasMany = [followers: User]
  static belongsTo = User
}

请查看this以获取有关mappedBy的更多信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接