1

我目前正在尝试基于旧的 MySQL 数据库创建一个新的 Grails 应用程序。应用程序应该只读取信息。具体的 DB 模式为特定的域类使用一个表,每个类层次结构,以及一个属性类,以向这些类添加新的所需信息。

目前我无法检索transation. 也不例外,但我也无法访问该字段properties。我可能面临的一个问题是,这个词properties是 Grails 的域字段的关键字。但由于特定的遗留表命名,我需要使用它。

遗留表被命名为transactiontransaction_properties。一个transcation可以有多个transaction_properties。关联是通过表transaction_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 中默认完成的。

在 Transaction 域类中删除这些列映射:

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 回答