Grails multi datasource domain issue

后端 未结 1 430
有刺的猬
有刺的猬 2021-01-06 17:21

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

相关标签:
1条回答
  • 2021-01-06 17:32

    You should use newer docs, e.g. 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. The same problem happens when you use Hibernate and a NoSQL GORM plugin.

    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']
    }
    

    Since Groovy treats getter/setter pairs as a property, you would use it like a "real" link:

    def foo = ...
    def bar = foo.provider.bar
    
    0 讨论(0)
提交回复
热议问题