-1

我们在Grails中创建了两个不同的域对象,并试图从两个不同的架构访问。无法自定义Grail&Groovy中的域类架构

方法1:

例如:

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连接字符串中删除默认模式,我得到“未选择数据库”日志中的错误。

方法2:

我们试图来配置多个数据源选择每个模式中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

任何人都可以提出一个解决的办法?

回答

0

在你application.yml文件,使用数据源:下方的默认数据源是这样的:

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 

现在,您的客户类应该看起来像

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

对于进一步的细节,你可以看到Multiple Datasources In Grails,official doc