【问题标题】:grails gorm way to store the date a many-to-many relationshp was createdgrails gorm 存储创建多对多关系的日期的方式
【发布时间】:2016-12-14 21:23:59
【问题描述】:

假设我有这两个域类....

class Design{
    static hasMany=[contributors:Contributor]

}

class Contributor{
   static hasMany=[designs:Design]
   static belongsTo=Design

}

随着时间的推移,我使用

将贡献者添加到设计中
def design=Design.get(params.designId)
...
design.addToContributors(newContributor).

假设我想跟踪在贡献者和设计之间创建关系的日期。如果没有 grails gorm 抽象,我会在关系表中添加一个日期列

 design_rel_contributor
 | id | design_id | contributor_id | date_created |

但我不知道如何为 grails 中的多对多关系实现这种“元数据”。

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    您可以简单地添加dateCreated 列,然后通过实现beforeInsert() 来填充它:

    class Contributor {
        Date dateCreated
        static hasMany = [designs:Design]
        static belongsTo = Design
    
        static constraints = {
            dateCreated nullable: true
        }
    
        void beforeInsert() {
            dateCreated = new Date()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 2020-04-08
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      相关资源
      最近更新 更多