Grails多数据源域问题 [英] Grails multi datasource domain issue
问题描述
我有一个项目,表在两个数据源之间。
我在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'时无法解析对bean'sessionFactory'的引用。嵌套异常是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?
非常感谢。
我会试试。
这不会那么简单,因为公司域链接回供应商
它只需要更多的efort
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屋!