Grails中可能加入mongo和mysql域吗? [英] Is it possible in Grails to join mongo and mysql domains?

查看:124
本文介绍了Grails中可能加入mongo和mysql域吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MySQL db的域,其他域使用MongoDB。我可以加入他们吗?



例如:

上诉(mongo域名)

  class Appeal {

static mapWith =mongo

组织结构< === MySQL域(
...

组织(MySQL域)

  class组织{
...
static hasMany = [appeals:Appeal]; < == join mongo domain
}

例外情况是:

 初始化应用程序时出错:创建名为'transactionManagerPostProcessor'的bean时出错:bean初始化失败; 
嵌套异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为'transactionManager'的bean:设置构造函数时无法将引用
解析为bean'$ primaryTransactionManager'带键[0]的
参数;嵌套异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为'$ primaryTransactionManager'的bean:设置bean属性$ b $时无法解析
引用bean'sessionFactory' b'sessionFactory';嵌套的异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为'sessionFactory'的bean:调用init方法
失败;嵌套异常是org.hibernate.MappingException:
关联引用未映射类:lc.itgroup.education.dao.Appeal
消息:创建名为'transactionManagerPostProcessor'的bean时出错:Bean初始化失败;
嵌套异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为'transactionManager'的bean:设置构造函数时无法将引用
解析为bean'$ primaryTransactionManager'带键[0]的
参数;嵌套异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为'$ primaryTransactionManager'的bean:设置bean属性$ b $时无法解析
引用bean'sessionFactory' b'sessionFactory';嵌套的异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为'sessionFactory'的bean:调用init方法
失败;嵌套异常是org.hibernate.MappingException:
关联引用未映射类:lc.itgroup.education.dao.Appeal


解决方案

不,但您可以近似它。在两个不同的关系数据库中连接两个表会产生同样的问题 - 每个数据库都有自己的 SessionFactory ,并且Hibernate或GORM不支持跨数据库或数据存储区加入。 p>

要近似它,请存储其他表/文档的主键,并使用瞬态方法为您检索实例。这基本上是Hibernate为你做的 - 它存储外键值并按需自动加载实例。

  class Appeal {

static mapWith =mongo

void setOrganization(Organization organization){
organizationId = organization.id
}
组织getOrganization (){
organizationId? Organization.get(organizationId):null
}
static transients = ['organization']

Long organizationId
...
}

使用这种方法,您的代码将非常类似于两个表都在同一个数据库中时的样子。当您访问组织时,它将使用之前保存的ID从数据库中检索。



这很重要组织属性是短暂的,因为像这样匹配的get / set对将被认为是一个持久属性,正如你所见,这将失败。只有id应该被保留。


I have one domain using MySQL db, other domain uses MongoDB. Can I join them?

For example:

Appeal (mongo domain)

class Appeal {

    static mapWith = "mongo"

    Organization organization <=== MySQL domain
    ...
}

Organization (MySQL domain)

class Organization {
    ... 
    static hasMany = [ appeals : Appeal ]; <==join to mongo domain
}

Exception is :

Error initializing the application: 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 '$primaryTransactionManager' while setting constructor
argument with key [0]; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name '$primaryTransactionManager': 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: lc.itgroup.education.dao.Appeal
    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 '$primaryTransactionManager' while setting constructor
argument with key [0]; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name '$primaryTransactionManager': 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: lc.itgroup.education.dao.Appeal

解决方案

No, but you can approximate it. You would have the same problem joining two tables in two different relational databases - each gets its own SessionFactory and there's no support in Hibernate or GORM to join across databases or datastores.

To approximate it, store the primary key of the other table/document and use transient methods to retrieve the instance for you. This is basically what Hibernate does for you - it stores the foreign key value and auto-loads the instance on demand.

class Appeal {

    static mapWith = "mongo"

    void setOrganization(Organization organization) {
        organizationId = organization.id
    }
    Organization getOrganization() {
        organizationId ? Organization.get(organizationId) : null
    }
    static transients = ['organization']

    Long organizationId
    ...
}

Using this approach, your code will be very similar to what it would look like if both tables were in the same database. When you access the Organization it will be retrieved from the database using the id that was previously persisted.

It's important that the organization property be transient since having a matched get/set pair like this would be considered a persistent property and as you've seen, this will fail. Only the id should be persisted.

这篇关于Grails中可能加入mongo和mysql域吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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