简体   繁体   English

Grails多数据源域问题

[英]Grails multi datasource domain issue

I have a project which tables spread between 2 datasources. 我有一个项目,表格分布在2个数据源之间。 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 我正在根据grails文档http://grails.org/doc/2.0.0.M2/guide/conf.html#dataSourcesAndEnvironments中的 3.3.6主题配置访问表的代码

Everything seems to be ok, but I got the following error 一切似乎都没问题,但我得到了以下错误

Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; 消息:创建名为'transactionManagerPostProcessor'的bean时出错:bean的初始化失败; 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'; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'transactionManager'的bean时出错:在设置bean属性'sessionFactory'时无法解析对bean'sessionFactory'的引用; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'sessionFactory'的bean时出错:init方法的调用失败; nested exception is org.hibernate.MappingException: Association references unmapped class: br.com.fisgo.Provider 嵌套异常是org.hibernate.MappingException:关联引用未映射的类:br.com.fisgo.Provider

Caused by MappingException: Association references unmapped class: br.com.fisgo.Provider 由MappingException引起:关联引用未映射的类:br.com.fisgo.Provider

Any idea on why do I get this error? 我知道为什么会出现这个错误?

Regards. 问候。

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]

You should use newer docs, eg http://grails.org/doc/latest/guide/conf.html#dataSourcesAndEnvironments 您应该使用较新的文档,例如http://grails.org/doc/latest/guide/conf.html#dataSourcesAndEnvironments

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. 这是不可能的,因为每个DataSource都有一个单独的SessionFactory ,它们不能直接一起工作。 The same problem happens when you use Hibernate and a NoSQL GORM plugin. 使用Hibernate和NoSQL GORM插件时会出现同样的问题。

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): 给定一个需要引用Provider的域类Foo,你可以持久保存外键并按需查找(这正是Hibernate在两个域类之间加入时为你做的):

class Foo {
   Long providerId

   Provider getProvider() {
      providerId ? Provider.get(providerId) : null
   }
   void setProvider(Provider provider) {
      providerId = provider.id
   }
   static transients = ['provider']
}

Since Groovy treats getter/setter pairs as a property, you would use it like a "real" link: 由于Groovy将getter / setter对视为属性,因此您可以像使用“真实”链接一样使用它:

def foo = ...
def bar = foo.provider.bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM