繁体   English   中英

用于单向1:n关系的Grails GORM数据库映射

[英]Grails GORM database mapping for unidirectional 1:n relationship

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

目前,我无法检索transation的属性信息。 也不例外,但我也无法访问字段properties 我可能会遇到的一个问题是,单词properties是Grails域域的关键字。 但是由于特定的旧表命名,我需要使用它。

遗留表分别命名为transactiontransaction_properties 一个transcation可以具有多个transaction_properties 关联是通过transaction_properties表中的键transaction_id完成的。

交易

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

您的代码是一团糟。

您需要在TransactionProperty域类中添加一个static belongsTo = [transaction: Transaction] 这将告诉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()
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM