Tobias Kraft
Tobias Kraft

Reputation: 348

Avoiding join table in grails

Considering the following simplified situation: I have a product that can have serveral attributes and the values of the attributes depend on the country.

When I model the attribute domain classes the standard way it looks like shown below. This means an attribute can have values that depend on a country and a value can belong to different attributes. The n:m relation results in a separate join table.

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
}

The question is now if there is a way to model this in GORM without a join table?

With SQL native it is no problem. In the database this would result in a structure like shown below. The join should go from the column attribute.attribute_key to the column attribute_value.attribute_key

enter image description here

Upvotes: 0

Views: 278

Answers (1)

Lonyui
Lonyui

Reputation: 101

join table in dataBase

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

and then create grails.domain class

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:""
    }
}

Use GORM to get domain Class

Upvotes: 1

Related Questions