Jigar Joshi
Jigar Joshi

Reputation: 240956

Grails mapping issue - many to many

I have 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 it shows

Caused by: org.codehaus.groovy.grails.exceptions.GrailsDomainException: No owner defined between domain classes [class User] and [class Post] in a many-to-many relationship. Example: static belongsTo = Post

Version Grails 1.3.7

Upvotes: 4

Views: 936

Answers (1)

alonmo
alonmo

Reputation: 83

I had the same problem, meaning creating a many to many relationship AND a 1 to many relationship between the same two classes.

The way to do that is as following:

User class:

class User{

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

Post class:

class Post{  

  User creator
  static hasMany = [subscribers: User]
  static belongsTo = User
}

I've found that answer in this discussion

Upvotes: 5

Related Questions