Grails:映射同一类型的字段和belongsTo的列名称 [英] Grails: mapping column names of a field and a belongsTo of the same type

查看:193
本文介绍了Grails:映射同一类型的字段和belongsTo的列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图映射这个类的列名:
$ b

  class Amount { 

字符串总数//总数量
字符串类型//金额类型,美元,%,时间
Bonification bonificationRate //金额可以是x times another bonification

static belongsTo = [parentBonification:Bonification]

static mapping = {
表AMOUNTS
总列数:AMOU_TTOTAL
类型列:AMOU_TTYPE
parentBonification列:BONI_CID
bonificationRate列:BONI_TRATE_CID
}

}


class Bonification {
//其中包括:
金额
}

这个东西不是带有字段的字段,在数据库中创建了Bonification 类,
不是我给它们的名字,也不是与默认的suppossed名称。

评论编辑:


  1. 两者都是域类;

  2. 数据源位于 dbCreate =update

  3. 让Grails再创造一次。仍然没有Bonification列。

  4. 我无法 dbCreate =create-drop,因为我无法删除的数据现在。

  5. 我使用 dbCreate = create-drop 装入了一个新的本地Derby数据库。仍然没有运气。

(PD:所有其他字段被持久化并且用正确的列名映射,只有那两个 Bonification 字段是问题)

是否有另外一种方法来做到这一点?


Grails:1.3.9

BD:Oracle 9


为DataSource.groovy进行编辑
(从Config.groovy
grails.config访问的外部文件。 locations = [file:$ {extConfig} / $ {appName} Config.groovy]

 包xx.xxx.xxxx.xxxXxxx 

dataSource
{
pooled = true
dialect =org.hibernate.dialect.Oracle10gDialect
driverClassName =oracle.jdbc.OracleDriver
username =XXX
password =XXX
properties {
maxActive = 100
maxIdle = 25
minIdle = 5
initialSize = 5
minEvictableIdleTimeMillis = 60000
timeBetweenEvictionRunsMillis = 60000
maxWait = 10000
validationQuery =从双
中选择1

}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.provider_class ='net.sf.ehcache.hibernate.EhCacheProvider'
}

//环境特定设置
环境{
开发{
println包含外部数据源
dataSource {
dbCreate =update
url =jdbc:oracle:thin:@ xxx.xxx.xx:xxxx:XXXX
}
}
}

解决方案

好吧,最后我明白了。

mappedBy属性。



http://grails.org/doc/2.1.0/ref/Domain%20Classes/mappedBy.html



文档(< 6.2.1.2一对多)表示这是用于hasMany 一面。这使我困惑,因为我没有一个hasMany,而是两个 Class Amount 类型的字段。
我真正必须做的是使用mappedBy属性,而不是在Amount类中,而是在Bonification类中只有一个对 Amount 的引用。


然后,我告诉金额类,至于Bonification,他必须反引用,所以he不会不会感到困惑,并且将 bonificationRate 字段视为字段,而不是我之前所做的参考。

  class金额{

字符串总数//总金额
字符串类型//金额类型,美元,%,Times东西
Bonification bonificationRate //数量可以是x倍另一个优化

static belongsTo = [parentBonification:Bonification]

static mapping = {
表AMOUNTS
总列:AMOU_TTOTAL
类型列:AMOU_TTYPE
parentBonification列:BONI_CID
bonificationRate列:BONI_TRATE_CID
}

}


类别Bonification {
//其中包括:
金额金额

static mappedBy = [金额: parentBonification]
}

没有创建 parentBonificationBONI_CID列但它创建了 bonificationRateBONI_TRATE_CID,我需要它。



对不起,太乱了。感谢您的时间和帮助。


I'm trying to map the column names of this class:

class Amount{

    String total //Total amount of something
    String type  //Type of amount, Dollars, %, Times something
    Bonification bonificationRate  //the amount can be "x times another bonification"

    static belongsTo = [parentBonification: Bonification]

    static mapping = {
       table "AMOUNTS"
       total column: "AMOU_TTOTAL"
       type column: "AMOU_TTYPE"
       parentBonification column: "BONI_CID"
       bonificationRate column: "BONI_TRATE_CID"
    }

}


class Bonification {  
    //among other things:  
    Amount amount
}

The thing is none of the field with the Bonification class are created in the DB, not with the name I give them and neither with the default suppossed names.

Comment Edit:

  1. Both are Domain Classes;
  2. Datasource is on dbCreate = "update"
  3. I dropped the Table in Oracle and let Grails create it again. Still no Bonification columns.
  4. I can not dbCreate = "create-drop" because there is data that I can't delete for now.
  5. I mounted a new local Derby Database with dbCreate = create-drop. Still no luck.

(PD: All other fields are persisted and mapped with the right column names, only those two Bonification fields are the problem)
Is there another way of doing this?

Grails: 1.3.9
BD: Oracle 9

Edit for asked DataSource.groovy (External file accessed from Config.groovy grails.config.locations = [ "file:${extConfig}/${appName}Config.groovy"])

package xx.xxx.xxxx.xxxXxxx

dataSource 
{
    pooled = true
    dialect = "org.hibernate.dialect.Oracle10gDialect"
    driverClassName = "oracle.jdbc.OracleDriver"    
    username = "XXX"
    password = "XXX" 
    properties {
                maxActive = 100
                maxIdle = 25
                minIdle = 5
                initialSize = 5
                minEvictableIdleTimeMillis = 60000
                timeBetweenEvictionRunsMillis = 60000
                maxWait = 10000
                validationQuery = "select 1 from dual"
                }

}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = true
    cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
}

// environment specific settings
environments {
    development {
println "Including external DataSource"
        dataSource {            
            dbCreate = "update"
            url = "jdbc:oracle:thin:@xxx.xxx.xx:xxxx:XXXX"
        }
    }
}

解决方案

Ok, finally I got it.

I used the "mappedBy" property.

http://grails.org/doc/2.1.0/ref/Domain%20Classes/mappedBy.html

The documentation (6.2.1.2 One-to-many) said that is for use in the "hasMany" side. That confused me because I didn't have a hasMany but rather two field of the same type in Class Amount. And what I really had to do was to use the mappedBy property, not in the Amount Class, but in the Bonification Class that has just one reference to Amount.

And with that I tell to the Amount Class, wich Bonification he has to back reference so "he" doesn't get confused, and take the bonificationRate field just as a field and not as a reference as I think he was doing before.

class Amount{

    String total //Total amount of something
    String type  //Type of amount, Dollars, %, Times something
    Bonification bonificationRate  //the amount can be "x times another bonification"

    static belongsTo = [parentBonification: Bonification]

    static mapping = {
       table "AMOUNTS"
       total column: "AMOU_TTOTAL"
       type column: "AMOU_TTYPE"
       parentBonification column: "BONI_CID"
       bonificationRate column: "BONI_TRATE_CID"
    }

}


class Bonification {  
    //among other things:  
    Amount amount

    static mappedBy = [amount: "parentBonification"]
}

That didn't created the parentBonification "BONI_CID" column but it Did created the bonificationRate "BONI_TRATE_CID" wich I needed.

Sorry if it's too messy. Thanks for the time and help anyway.

这篇关于Grails:映射同一类型的字段和belongsTo的列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆