简体   繁体   English

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

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

I am currently trying to create a new Grails application based on a legacy MySQL database. 我目前正在尝试基于旧版MySQL数据库创建新的Grails应用程序。 The application should only read information. 该应用程序应仅读取信息。 The concrete DB schema uses for the specific domain classes a table per class hierarchy structure, as well as a property class to add new needed information to these classes. 具体的DB模式针对特定的域类使用每个类层次结构的表以及用于向这些类添加新的所需信息的属性类。

Currently I cannot retrieve the property information for a transation . 目前,我无法检索transation的属性信息。 There is no exception, but I also cannot access the field properties . 也不例外,但我也无法访问字段properties One problem I may face is, that the word properties is a keyword for Grails for the domain fields. 我可能会遇到的一个问题是,单词properties是Grails域域的关键字。 But I need to use it because of the specific legacy table naming. 但是由于特定的旧表命名,我需要使用它。

The legacy tables are named transaction and transaction_properties . 遗留表分别命名为transactiontransaction_properties One transcation can have muliple transaction_properties . 一个transcation可以具有多个transaction_properties The association is done via the key transaction_id in the transaction_properties table. 关联是通过transaction_properties表中的键transaction_id完成的。

Table transaction 交易

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

Table transaction_properties transaction_properties

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

Domain class Transaction 领域类交易

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

Domain class TransactionProperty 领域类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}"
}
   }

Your code is a giant mess. 您的代码是一团糟。

You need to add a static belongsTo = [transaction: Transaction] in your TransactionProperty domain class. 您需要在TransactionProperty域类中添加一个static belongsTo = [transaction: Transaction] This will tell grails to use the foreign key in that table instead of wanting a join table. 这将告诉grails在该表中使用外键,而不需要连接表。

You also do not need to specify Long id in either table. 您也不需要在任何一个表中指定Long id That is done by default in Grails. 默认情况下,这在Grails中完成。

Remove these column mappings as well in the Transaction domain class: 在Transaction域类中也删除这些列映射:

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

In TransactionProperty, it technically does not have a primary key (unless you are hiding it from us), so you will have to use a composite key of property_key and property_value. 在TransactionProperty中,从技术上讲,它没有主键(除非您向我们隐藏了它),因此您将必须使用property_key和property_value的复合键。

id name : "transaction_id"

should be: 应该:

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

Read up on the additional requirements of this here: http://grails.org/doc/latest/guide/GORM.html#5.5.2.5%20Composite%20Primary%20Keys 在此处阅读有关此内容的其他要求: http : //grails.org/doc/latest/guide/GORM.html#5.5.2.5%20Composite%20Primary%20Keys

My best attempt at fixing your classes: 我尽最大努力解决您的问题:

Transaction: 交易:

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

}

TransactionProperty: 交易属性:

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