Grails 映射问题 - 多对多

发布于 2024-12-29 05:50:12 字数 663 浏览 4 评论 0原文

我有以下结构

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]
   } 

,它显示

原因: org.codehaus.groovy.grails.exceptions.GrailsDomainException:没有所有者 定义在域类 [class User] 和 [class Post] 之间 多对多关系。示例:静态belongsTo = Post

  • 一个用户可以有多个帖子
  • 一个帖子属于一个用户
  • 一个帖子可以有多个订阅者

版本 Grails 1.3.7

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

  • A user can have multiple post
  • A Post belongs to a User
  • A Post can have multiple subscribers

Version Grails 1.3.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别想她 2025-01-05 05:50:12

我遇到了同样的问题,这意味着在相同的两个类之间创建多对多关系和一对多关系。

执行此操作的方法如下:

用户类别:

class User{

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

帖子类别:

class Post{  

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

我在 此讨论

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文