I have the following two domain classes, User and Posts And I have two relationships between them, User has 1-to-many with Posts with back reference. User has many-to-many relationships with posts that he follows: The relationships I've got are as follows:
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
}
Now I'm getting a clash with Grails, I tried solving it through mapping but with no success, this is the error I get :
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)
Anyone know how to resolve this ?
I think you can do it using mappedBy, like:
class User{
static hasMany = [posts : Post, followingPosts: Post]
static mappedBy = [posts : "user"]
}
class Post{
User user
static hasMany = [followers: User]
static belongsTo = User
}
Take a look at this for more info about the mappedBy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With