Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following domains classes:

class Posts{
      String Name
      String Country
      static hasMany = [tags:Tags]

        static constraints = {
        }
    }


class Tags{

    String Name
    static belongsTo = Posts
    static hasMany = [posts:Posts]
    static constraints = {
    }
  String toString()
  {
      "${TypeName}"

  }
}

Grails creates an another table in the database i.e. Posts_Tags.
My requirement is:

E.g. 1 post has 3 tags. So, in the Posts_Tags table there are 3 rows.

How can I access the table Posts_Tags directly in my code so that I can manipulate the data or add some more fields to it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
687 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you want to access the join table (Posts_Tags) directly, or add properties to it, then you must define it as a separate PostTag domain class. You then split your many-many relationship between Post and Tag into 2 one-to-many relationships (one from Post to PostTag and one from Tag to PostTag).

Here's a comprehensive example of how to perform the mapping and add properties to the join table - in this example Membership is the join table.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...