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

Is it possible in Grails to join mongo and mysql domains?

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

例如:

申诉(mongo 域)

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

没有,但你可以近似。在连接两个不同的关系数据库中的两个表时,您会遇到同样的问题——每个表都有自己的 SessionFactory,并且 Hibernate 或 GORM 不支持跨数据库或数据存储连接。

为了近似它,存储另一个 table/document 的主键并使用瞬态方法为您检索实例。这基本上就是 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 应该被持久化。