Reputation: 121
Two of my domain classes (User
and Post
) have multiple one-to-many associations between them. User
may like, dislike, see or own multiple Post
s. For this I have set up hasMany relationship as following:
static hasMany = [posts:Post, seen:Post, likes:Post, dislikes:Post]
After developing owning side of my code when I started to work with seen
I saw that each object that is in posts
list is also in seen
, likes
and dislikes
. Then I tried to use joinTable to map tables:
static mapping = {
posts joinTable: [name: 'USER_POSTS',
column: 'POST_ID',
key: 'USER_ID']
seen joinTable: [name: 'USER_SEEN',
column: 'POST_ID',
key: 'USER_ID']
likes joinTable: [name: 'USER_LIKES',
column: 'POST_ID',
key: 'USER_ID']
dislikes joinTable: [name: 'USER_DISLIKES',
column: 'POST_ID',
key: 'USER_ID']
}
But unfortunately nothing changed, posts added to posts
still go to other lists too. What am I doing wrong?
Upvotes: 1
Views: 443
Reputation: 121
After trying many mappedBy
and mapping
stuff I decided to create separate domains (actually with the same properties) to keep the liking, disliking and seeing connection between User
and Post
. Actually my solution isn't relationship based, it is a kind of log of User-Post activities.
class UserLike {
static belongsTo = [user:User,post:Post]
}
class UserDislike {
static belongsTo = [user:User,post:Post]
}
class UserSee {
static belongsTo = [user:User,post:Post]
}
When I want to make some post1
liked by user1
I do:
new UserLike(user:user1,post:post1).save()
Maybe it isn't not be perfect for some others but solved my problem
Upvotes: 1