【问题标题】:Avoiding join table in grails避免在 grails 中连接表
【发布时间】:2019-04-10 11:49:13
【问题描述】:

考虑以下简化情况: 我的产品可以具有多个属性,并且属性的值取决于国家/地区。

当我以标准方式对属性域类建模时,它看起来如下所示。这意味着属性可以具有取决于国家/地区的值,并且值可以属于不同的属性。 n:m 关系产生一个单独的连接表。

class Attribute  {
    String id
    String name

    static hasMany = [attributeValues: AttributeValue]

    static mapping = {
        attributeValues joinTable: [name: 'attribute_values', key: 'attribute_id']
    }
}


class AttributeValue {
    String id
    Locale language
    String value
}

现在的问题是,是否有一种方法可以在没有连接表的情况下在 GORM 中对此进行建模?

使用 SQL native 没问题。在数据库中,这将导致如下所示的结构。 连接应该从列属性.attribute_key 到列attribute_value.attribute_key

【问题讨论】:

    标签: hibernate grails groovy grails-orm


    【解决方案1】:

    连接数据库中的表

    create view JOIN_ATTRIBUTE_VALUE_V as
    (!!the sql u select upown!!)
    

    然后创建 grails.domain 类

    class joinAttributeValueV {
        String id
        String attributeKey
        Locale language
        String value
    
        static mapping = {
            table 'JOIN_ATTRIBUTE_VALUE_V'
            id            column:"id" ,comment:""
            attributeKey  column:"ATTRIBUTE_KEY" ,comment:""
            language      column:"LANGUAGE" ,comment:""
            value         column:"VALUE" ,comment:""
        }
    }
    

    使用GORM获取域类

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-24
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      相关资源
      最近更新 更多