我有一个域使用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/