Grails 多数据源域问题 [英] Grails multi datasource domain issue
问题描述
我有一个项目,其中的表分布在 2 个数据源之间.我正在按照 grails 文档中的 3.3.6 主题配置代码以访问表http://grails.org/doc/2.0.0.M2/guide/conf.html#dataSourcesAndEnvironments
I have a project which tables spread between 2 datasources. I'm configuring the code to access table as per 3.3.6 topic in grails documentations http://grails.org/doc/2.0.0.M2/guide/conf.html#dataSourcesAndEnvironments
似乎一切正常,但出现以下错误
Everything seems to be ok, but I got the following error
消息:创建名为transactionManagerPostProcessor"的 bean 时出错:bean 初始化失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为transactionManager"的 bean 时出错:设置 bean 属性sessionFactory"时无法解析对 beansessionFactory"的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为sessionFactory"的 bean 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.MappingException:关联引用未映射的类:br.com.fisgo.Provider
Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Association references unmapped class: br.com.fisgo.Provider
由 MappingException 引起:关联引用未映射的类:br.com.fisgo.Provider
Caused by MappingException: Association references unmapped class: br.com.fisgo.Provider
知道为什么会出现此错误吗?
Any idea on why do I get this error?
问候.
我会试试的.它不会那么简单,因为公司域链接回提供商只是需要更多的努力
I'll try it out. It won't be that simple because Company domain links back to Provider It will just require more efort
class Company {
String name
String cnpj
String email
Address address
Phone phone
String registration
String source
Set provider = new HashSet<Provider>()
static hasMany = [provider: Provider]
推荐答案
您应该使用较新的文档,例如http://grails.org/doc/latest/guide/conf.html#dataSourcesAndEnvironments
You should use newer docs, e.g. http://grails.org/doc/latest/guide/conf.html#dataSourcesAndEnvironments
您似乎正在尝试跨数据源进行链接.这是不可能的,因为每个 DataSource
都有一个单独的 SessionFactory
,并且它们不能直接一起工作.当您使用 Hibernate 和 NoSQL GORM 插件时,也会发生同样的问题.
It looks like you're trying to link across datasources. This isn't possible since each DataSource
has a separate SessionFactory
, and they cannot work directly together. The same problem happens when you use Hibernate and a NoSQL GORM plugin.
不过你可以很容易地模仿它.给定一个需要引用 Provider
的域类 Foo,您可以保留外键并按需查找它(当您在两个域类之间加入时,这正是 Hibernate 为您所做的):
You can mimic it easily enough though. Given a domain class Foo that needs a reference to Provider
, you can persist the foreign key and look it up on-demand (and this is really what Hibernate does for you when you join between two domain classes):
class Foo {
Long providerId
Provider getProvider() {
providerId ? Provider.get(providerId) : null
}
void setProvider(Provider provider) {
providerId = provider.id
}
static transients = ['provider']
}
由于 Groovy 将 getter/setter 对视为属性,因此您可以像使用真实"链接一样使用它:
Since Groovy treats getter/setter pairs as a property, you would use it like a "real" link:
def foo = ...
def bar = foo.provider.bar
这篇关于Grails 多数据源域问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!