Search code examples
grailsgrails-orm

How can I get dateCreated in Has-Many Relation in Grails?


I have the following domain classes:

class A {
 hasMany = [bs : B]

}

class B { }

Note that B has no backward relation to a. GORM creates a join table in my MYSQL database a_b. This table has two columns the id of a and the id of b.

How can I get a dateCreatedin the join table?


Solution

  • Create a model of the join table yourself and add whatever properties you want on it. Simple, and done.

    For example:

    class A {
      static hasMany = [bs: JoinB]
    }
    
    class JoinB {
      static belongsTo = [a: A]
      B b
      Date dateCreated
    
      static mapping = {
        autoTimestamp true // default, but I like to be explicit about it.
      }
    }
    
    class B {
      String whatever
    }
    

    (Careful of typos etc. I just did that off the top of my head)