【问题标题】:Unable to customize the schema for a domain class in Grail & Groovy无法在 Grails 和 Groovy 中自定义域类的架构
【发布时间】:2017-10-21 07:26:39
【问题描述】:

我们在 Grails 中创建了两个不同的域对象,并尝试从两个不同的模式进行访问。

方法一:

例如:

Student.groovy

class Students {
     String id
     String name
     String address 

   Static mapping = {
     schema: 'student_details' 
   } 
 }

Customer.groovy

class Customer {
    String firstName
    String lastName 
    String address  

   Static mapping = {
      schema: 'customer_details'
    }    
 }

application.yml

 environments:
    development:
       dataSource:
          dbCreate: update
          url: jdbc:mysql://localhost:3306/

如果我在 url 连接字符串中提供默认架构,它总是引用该默认架构,而不管域类中定义的架构并抛出异常,找不到表。如果我从 url 连接字符串中删除默认架构,我会在日志中收到 "No database selected" 错误。

方法二:

我们尝试在 application.yml 中为每个模式配置多个数据源选项,如下所示:

dataSource:
    pooled: true
    dbCreate: update
    url: jdbc:mysql://localhost:3306/sample_grails
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    username: root
    password: ''
secondary:
    pooled: true
    dbCreate: update
    url: jdbc:mysql://localhost:3306/grails_mapping
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    username: root
    password: ''

使用域类作为Customer.groovy

class Customer {
    String firstName
    String lastName 
    String address  

   Static mapping = {
      datasource 'secondary'
   }     
 }

我遇到了一个错误

Caused by: org.grails.datastore.mapping.core.exceptions.ConfigurationException: DataSource not found for name [secondary] in configuration. Please check your multiple data sources configuration and try again.

我们参考了以下链接以进行多模式访问:

https://objectpartners.com/2016/03/09/using-secondary-datasources-in-grails-3/

Creating a Domain Class with schema in Grails

任何人都可以提出解决方案吗?

【问题讨论】:

    标签: groovy schema grails-domain-class grails-3.3 multiple-schema


    【解决方案1】:

    您的方法 2 几乎就在那里,我认为您缺少的是-您需要在“数据源”下定义“辅助”-请参阅此-http://docs.grails.org/latest/guide/conf.html#multipleDatasources

    【讨论】:

      【解决方案2】:

      在您的 application.yml 文件中,以这种方式在默认数据源下方使用 datasources:

      dataSource:
         pooled: true
         dialect: org.hibernate.dialect.MySQL5InnoDBDialect
         driverClassName: com.mysql.jdbc.Driver
         dbCreate: update
         url: jdbc:mysql://localhost:3306/default_schema
         username: root
         password: ''
      datasources:
        source1:
          dialect: org.hibernate.dialect.MySQLInnoDBDialect
          driverClassName: com.mysql.jdbc.Driver
          username: root
          password: ''
          url: mysql://localhost:3306/source1
          dbCreate: update
      

      现在,您的 Customer 类应该是这样的

      class Customer {
      String firstName
      String lastName 
      String address
      Static mapping = {
        datasource 'source1'
      }     
      }
      

      更多详情,您可以查看Multiple Datasources In Grails,official doc

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-02
        • 2016-05-05
        • 1970-01-01
        相关资源
        最近更新 更多