1

現在、レガシー MySQL データベースに基づいて新しい Grails アプリケーションを作成しようとしています。アプリケーションは情報を読み取るだけです。具体的な DB スキーマは、特定のドメイン クラスに対して、クラス階層構造ごとのテーブルと、これらのクラスに必要な新しい情報を追加するためのプロパティ クラスを使用します。

現在、 のプロパティ情報を取得できませんtransation。例外はありませんが、私もフィールドにアクセスできませんproperties。私が直面する可能性のある問題の 1 つは、単語propertiesが Grails のドメイン フィールドのキーワードであるということです。しかし、特定の従来のテーブルの命名のために、それを使用する必要があります。

レガシー テーブルの名前はtransactionおよびtransaction_propertiesです。複数transcation持つことができますtransaction_propertiestransaction_id関連付けは、テーブル内のキーを介して行われtransaction_propertiesます。

テーブルトランザクション

id  bigint(20)
transaction_id  varchar(255) (bad naming here, transaction_id is used to store additional meta information)

テーブルtransaction_properties

transaction_id  bigint(20) -> referencing to transation.id
property_value  varchar(255)
property_key    varchar(32)
etc.

ドメイン クラストランザクション

class Transaction {

static hasMany = [properties : TransactionProperty]

static constraints = {
    //   transactionProperty unique: true
}

static mapping = {
    table "transaction"
    version false
    columns {
        id column : "id"
        beginDate column : "start"
        endDate column : "end"
        type column : "DTYPE"
        amount column : "total_amount"
        metaId column : "transaction_id"
        purchase column : "purchase_id"
        service column : "service_id"
        origin column : "origin_id"
        properties column : "id"
    }

}

Long id
Date beginDate
Date endDate
String type
String amount
String metaId

Purchase purchase
Origin origin
Service service
  etc.
  }

ドメイン クラスTransactionProperty

  class TransactionProperty {

static mapping = {
    table "transaction_properties"
    version false
    columns {
        id name : "transaction_id"
        key column : "property_key"
        value column : "property_value"
    }
}

String value
String key
Long id

def asString(){
    return "${key} = ${value}"
}
   }
4

1 に答える 1

0

あなたのコードは大混乱です。

static belongsTo = [transaction: Transaction]TransactionProperty ドメイン クラスにを追加する必要があります。これにより、結合テーブルを必要とする代わりに、そのテーブルの外部キーを使用するように grails に指示されます。

Long idまた、どちらの表にも指定する必要はありません。これは、Grails ではデフォルトで行われます。

トランザクション ドメイン クラスで、これらの列マッピングも削除します。

id column : "id"
properties column : "id"

TransactionProperty では、技術的に主キーを持たないため (非表示にしている場合を除きます)、property_key と property_value の複合キーを使用する必要があります。

id name : "transaction_id"

次のようにする必要があります。

id composite: ['key', 'value']

この追加要件については、http: //grails.org/doc/latest/guide/GORM.html#5.5.2.5%20Composite%20Primary%20Keysを参照してください。

あなたのクラスを修正するための私の最善の試み:

取引:

class Transaction {

static hasMany = [properties : TransactionProperty]

static constraints = {
}

static mapping = {
    table "transaction"
    version false
    columns {
        beginDate column : "start"
        endDate column : "end"
        type column : "DTYPE"
        amount column : "total_amount"
        metaId column : "transaction_id"
        purchase column : "purchase_id"
        service column : "service_id"
        origin column : "origin_id"
    }

}

Date beginDate
Date endDate
String type
String amount
String metaId

Purchase purchase
Origin origin
Service service

}

トランザクション プロパティ:

import org.apache.commons.lang.builder.HashCodeBuilder

class TransactionProperty implements Serializable {

static belongsTo = [transaction: Transaction]

static mapping = {
    table "transaction_properties"
    version false
    columns {
        key column : "property_key"
        value column : "property_value"
    }
}

String value
String key


def toString(){
    return "${key} = ${value}"
}

boolean equals(other) {
    if (!(other instanceof TransactionProperty)) {
        return false
    }

    other.key == key && other.value == value
}

int hashCode() {
    def builder = new HashCodeBuilder()
    builder.append key
    builder.append value
    builder.toHashCode()
}
}
于 2012-12-17T20:13:13.937 に答える