gpt4 book ai didi

java - Grails 多对多和一对多冲突

转载 作者:搜寻专家 更新时间:2023-11-01 00:51:31 24 4
gpt4 key购买 nike

我有以下两个域类,用户和帖子我在他们之间有两种关系,用户与带有反向引用的帖子一对多。用户与他关注的帖子具有多对多关系:我得到的关系如下:

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)

有人知道怎么解决吗?

最佳答案

我认为您可以使用 ma​​ppedBy 来做到这一点,例如:

class User{

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


class Post{

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

看看this有关 ma​​ppedBy 的更多信息。

关于java - Grails 多对多和一对多冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13529682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com