避免在grails中使用连接表

问题描述 投票:0回答:1

考虑以下简化情况:我有一个产品可以有几个属性,属性的值取决于国家。

当我为属性域类建模时,它的标准方式如下所示。这意味着属性可以具有依赖于国家/地区的值,并且值可以属于不同的属性。 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.attribute_key到列attribute_value.attribute_key

enter image description here

hibernate grails groovy gorm
1个回答
1
投票

在dataBase中连接表

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获取域类

© www.soinside.com 2019 - 2024. All rights reserved.