跨两个数据源的Grails GORM域关联 [英] Grails GORM domain association across two datasources

查看:151
本文介绍了跨两个数据源的Grails GORM域关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果其他域类使用不同的数据源,是否有可能在两个域类(即 belongsTo )之间建立关联?这两个数据源也是不同的数据库驱动程序。



我怀疑这可能是不可能的,但我想在这里与社区联系,看看是否有可能。现在我试图做到这一点,我得到了通常怀疑Hibernate错误:

调用init方法失败;嵌套异常是org.hibernate.MappingException:来自表domain_class_A的关联引用了未映射的类:DomainClassB



示例:

  class DomainClassA {
static belongsTo = [dcB:DomainClassB]

static mapping = {
数据源ds1
表名:domain_class_A,架构:schema_A
}
}

类DomainClassB {
static hasMany = [ dcA:DomainClassA]

静态映射= {
datasourceds2
表名:domain_class_B,架构:schema_B
}
}


解决方案

正如@dmahapatro在他的评论中指出的那样,类似于1元素的情况,并且创建自己的方法来管理关系是一条路。这也与我之前关于映射集合的性能问题的演讲有关,所以你可以用一箭双雕杀死两只鸟: http://www.infoq.com/presentations/GORM-Performance



如果你不需要收集,即如果只用它来添加子对象的新实例,那么这将起作用,因为获得调用 DomainClassB 实例将使用它的数据源:

  class DomainClassA {
Long domainClassBId
private DomainClassB dcB

DomainClassB getDomainClassB(){
if(!dcB& domainClassBId){
dcB = DomainClassB.get(domainClassBId)
}
dcB
}

void setDomainClassB(DomainClassB dc){
domainClassBId = dc.id
}

static transients = ['domainClassB']

static mapping = {
datasourceds1
表名:domain_class_A,架构:schema_A
}
}

类DomainClassB {

static mapping = {
datasourceds2
表名:domain_class_B,架构:schema_B
}
}

创建新的DomainClassA实例与传统的 addTo ... 方法有点不同,但并不算太坏:

  DomainClassB dcb = ... 
def dca = new DomainClassA(domainClassBId:dcb .id)
dca.save()

如果您确实需要访问所有 DomainClassA DomainClassB 的实例,您可以添加一个方法:

  Set getDomainClassAs(){
DomainClassA.findAllByDomainClassBId(id)
}

但是,由于您自己正在执行查询,所以如果您只需要一些实例,则不必加载所有实例,所以你可以做任何你想要的查询。


Is it possible to have an association between two domain classes (i.e. belongsTo) if the other domain class uses a different datasource? The two datasources are different database drivers too.

I suspect this may not be possible, but I wanted to reach out to the community here to see if it was possible. Right now I'm trying to do it and I am getting the usual suspected Hibernate error:

Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from the table domain_class_A refers to an unmapped class: DomainClassB

Sample:

class DomainClassA {
    static belongsTo = [dcB: DomainClassB]

    static mapping = {
        datasource "ds1"
        table name: "domain_class_A", schema: "schema_A"
    }
}

class DomainClassB {
    static hasMany = [dcA: DomainClassA]

    static mapping = {
        datasource "ds2"
        table name: "domain_class_B", schema: "schema_B"
    }
}

解决方案

As @dmahapatro points out in his comment, this is similar to the 1-element case, and creating your own methods to manage the relationship is the way to go. This is also related to a talk I did a while back about performance issues with mapped collections, so you can kill two birds with one stone: http://www.infoq.com/presentations/GORM-Performance

If you don't need the collection, i.e. if you only use it to add new instances of the child object, then this will work because the get call for the DomainClassB instance will use its datasource:

class DomainClassA {
   Long domainClassBId
   private DomainClassB dcB

   DomainClassB getDomainClassB() {
      if (!dcB && domainClassBId) {
         dcB = DomainClassB.get(domainClassBId)
      }
      dcB
   }

   void setDomainClassB(DomainClassB dc) {
      domainClassBId = dc.id
   }

   static transients = ['domainClassB']

   static mapping = {
      datasource "ds1"
      table name: "domain_class_A", schema: "schema_A"
   }
}

class DomainClassB {

    static mapping = {
        datasource "ds2"
        table name: "domain_class_B", schema: "schema_B"
    }
}

Creating a new DomainClassA instance is a bit different from the traditional addTo... approach, but it's not too bad:

DomainClassB dcb = ...
def dca = new DomainClassA(domainClassBId: dcb.id)
dca.save()

If you do want access to all of the DomainClassA instances for a DomainClassB, you can add a method for that:

Set getDomainClassAs() {
   DomainClassA.findAllByDomainClassBId(id)
}

But since you're doing the query yourself, you don't have to load all of the instances if you only need some, so you can do whatever queries you want.

这篇关于跨两个数据源的Grails GORM域关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆