【发布时间】: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