mongodb - 在Grails中是否可以加入mongo和mysql域?

标签 mongodb grails gorm

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

例如:

上诉(mongo域)

class Appeal {

    static mapWith = "mongo"

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

组织(MySQL域)
class Organization {
    ... 
    static hasMany = [ appeals : Appeal ]; <==join to mongo domain
}

异常(exception)是:
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

最佳答案

不,但是您可以估算一下。在两个不同的关系数据库中连接两个表会遇到同样的问题-每个表都有自己的SessionFactory,并且Hibernate或GORM中不支持跨数据库或数据存储进行连接。

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

class Appeal {

    static mapWith = "mongo"

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

    Long organizationId
    ...
}

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

重要的是organization属性是 transient 的,因为具有这样的匹配的get / set对将被视为持久属性,并且如您所见,这将失败。只有ID应该被保留。

关于mongodb - 在Grails中是否可以加入mongo和mysql域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29515656/

相关文章:

java - 字符串加密 PHP/Java

grails - 如何在自定义Grails验证器中引用另一个属性?

class - Grails保存扩展类

sql - 具有空组件的复合ID的唯一性

grails - 更改表时Grails产生错误

c - C编程中的MongoDB聚合函数(不同)

javascript - mongoose 将字段从另一个文档添加到文档中

javascript - 什么是 MongoDB 的严格模式,使用它是个好主意吗?

C#/MongoDB : How do I keep a connection alive?

grails - 无法访问Spring Security UI的注册页面