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

时间:2015-04-08 13:12:42

标签: mongodb grails gorm

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

例如:

上诉(mongo domain)

class Appeal {

    static mapWith = "mongo"

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

组织(MySQL域名)

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

例外是:

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

1 个答案:

答案 0 :(得分:0)

不,但你可以近似它。在两个不同的关系数据库中连接两个表时会遇到同样的问题 - 每个表都有自己的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属性是暂时的非常重要,因为像这样的匹配的get / set对将被视为持久属性,并且正如您所见,这将失败。只应保留id。