【问题标题】:Grails multiple datasource, not working when trying to save a new Instance into selected datasourceGrails多个数据源,尝试将新实例保存到选定数据源时不起作用
【发布时间】:2016-03-16 13:56:27
【问题描述】:

我在处理多个数据源时遇到了一个简单配置的问题,我正在尝试将新实例保存到特定数据源中

我的数据源:

dataSources:
    dataSource:
        pooled: true
        jmxExport: true
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        driverClassName: com.mysql.jdbc.Driver
        username: root
        password: Choice2016
    dominio1:
        pooled: true
        jmxExport: true
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        driverClassName: com.mysql.jdbc.Driver
        username: root
        password: Choice2016
    dominio2:
        pooled: true
        jmxExport: true
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        driverClassName: com.mysql.jdbc.Driver
        username: root
        password: Choice2016

我的域名

class Nxtt_reports {

    Boolean favorite

    static hasMany = [nxtt_report_histories: Nxtt_report_history, nxtt_user_reports: Nxtt_user_reports, nxtt_report_snapshots: Nxtt_report_snapshot]

    static constraints = {

    }

    static mapping = {
        datasource 'ALL'
    }

}

当我这样做时,

    def nxtt = Nxtt_reports.class
    println(nxtt.dominio1.list())

我可以列出我想要的域中的数据,但如果我这样做

    def nxtt = Nxtt_reports.class.newInstance()
    nxtt.favorite = 0
    nxtt.dominio2.save()

我收到了

没有这样的属性:dominio2 类:nexttreport.server.Nxtt_reports。堆栈跟踪如下: groovy.lang.MissingPropertyException:没有这样的属性:dominio2 类:nexttreport.server.Nxtt_reports

使用:

| Grails 版本:3.1.1 | Groovy 版本:2.4.5 | JVM版本:1.8.0_65

编辑

environments:
    development:
        dataSources:
            dataSource:
                dbCreate: update
                url: jdbc:mysql://192.168.1.24:3306/nexttreport?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
            dominio1:
                dbCreate: update
                url: jdbc:mysql://192.168.1.24:3306/dominio1?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
            dominio2:
                dbCreate: update
                url: jdbc:mysql://192.168.1.24:3306/dominio2?useUnicode=yes&characterEncoding=UTF-8&useSSL=false

【问题讨论】:

  • 你能更新帖子并添加你的环境数据源配置吗?
  • 是的,我编辑了问题

标签: grails grails-orm grails-domain-class


【解决方案1】:

我在阅读和理解您的代码时遇到了一些麻烦,因为您没有遵循正常的 Java/Groovy 约定。

首先,我建议您使用普通的 CamelCase,并且在您的类名中不要使用下划线,因此请使用 NxxtReports

其次,为什么不 NxxtReports.domino1.list() 呢? list()NxxtReports 上的静态方法,所以这样称呼它。

第三,我认为最重要的是,使用普通的 Java/Groovy 方式来实例化一个对象,所以这样做

def nxxt = new NxxtReport()
nxxt.favorite = 0
nxxt.domino2.save()

我怀疑通过在 class 变量上调用 newInstance() 未正确添加 DomainClass 实例上的额外 Grails 方法。

我手头没有真正的计算机(现在在移动设备上),所以我无法检查我的代码,但我认为使用普通的 Java/Groovy 约定会有很大帮助。

【讨论】:

    猜你喜欢
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    相关资源
    最近更新 更多