Grail Mapping Problem - Many For Many

I have the following structure

class User{ List<Post> posts = new ArrayList<Post>(); static hasMany = [posts: Post] } 

  class Post{ User user List<User> subscribers = new ArrayList<User>(); static belongsTo = [user: User] static hasMany = [subscribers: User] } 

and he shows

Called: org.codehaus.groovy.grails.exceptions.GrailsDomainException: There is no owner between the classes of the class [class User] and [class Post] in a many-to-many relationship. Example: static belongs to To = Post

  • User can have multiple posts.
  • The message belongs to the user.
  • A message can have multiple subscribers

Grails Version 1.3.7

+4
source share
1 answer

I had the same problem, which means creating a many-many relationship and a 1-to-many relationship between the same two classes.

The way to do this is as follows:

User Class:

 class User{ static hasMany = [createdPosts: Post, subscribedToPosts : Post] static mappedBy = [createdPosts : "creator"] } 

Publication class

 class Post{ User creator static hasMany = [subscribers: User] static belongsTo = User } 

I found the answer in this discussion

+5
source

Source: https://habr.com/ru/post/1393481/


All Articles