2012-12-17 82 views
1

我目前正在嘗試基於舊版MySQL數據庫創建新的Grails應用程序。應用程序只應讀取信息。具體的數據庫模式針對特定的域類使用每個類層次結構的表,以及用於向這些類添加新所需信息的屬性類。針對單向1:n關係的Grails GORM數據庫映射

目前我無法檢索一個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}" 
} 
    } 
+0

你就不能更改的hasMany = [屬性:...]至[transactionProperties:...]?您沒有任何列映射到「屬性」一詞。它只是告訴Grails集合的名稱應該是什麼。 – Gregg

+0

當我這樣做時,我收到以下異常。 '造成的:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表 'KT-testing.transaction_transaction_properties' 不exist' – user1829893

回答

0

你的代碼是一個巨大的爛攤子。

您需要在您的TransactionProperty域類添加static belongsTo = [transaction: Transaction]。這將告訴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 

} 

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() 
} 
} 
+0

謝謝清除它。我希望能在聖誕節期間找到一些閱讀Grails in Action書籍的日子,我當然需要。這個固定我的問題:'類TransactionProperty實現Serializable { \t靜態映射= { \t \t表 「transaction_properties」 \t \t ID複合:[ '鍵', '值'] \t \t版本假 \t \t列{ \t \t \t鍵列: 「property_key」 \t \t \t值列: 「PROPERTY_VALUE」 \t \t} \t}' – user1829893