簡體   English   中英

Grails多對多和一對多的沖突

[英]Grails Many-To-Many and One-To-Many clash

我有以下兩個域類,用戶和帖子我和他們之間有兩個關系,用戶有一對多的帖子和后面的參考。 用戶與他所關注的帖子有多對多的關系:我得到的關系如下:

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
}

現在我和Grails發生了沖突,我嘗試通過映射解決它,但沒有成功,這是我得到的錯誤:

    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)

有誰知道如何解決這個問題?

我認為你可以使用mappedBy來做,比如:

class User{

  static hasMany = [posts : Post, followingPosts: Post]
  static mappedBy = [posts : "user"]
}


class Post{  

  User user
  static hasMany = [followers: User]
  static belongsTo = User
}

有關mappedBy的更多信息,請查看內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM