移植到 grails 3 时 MappingFactory 到 ToMany 的 ClassCastException

ClassCastException for MappingFactory to ToMany while porting to grails 3

当尝试 运行 命令 grails run-app --stacktrace 时,grails 抛出此错误:

org.springframework.beans.factory.BeanCreationException: 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 '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 java.lang.ClassCastException: org.grails.datastore.mapping.model.MappingFactory cannot be cast to org.grails.datastore.mapping.model.types.ToMany

我在所有域 类 中评论了 static hasMany,但错误仍然存​​在,直到我在我的 build.gradle[ 中将其替换为 hibernate3

如何使用 hibernate4 解决这个问题?

我找到了解决方案,实际上它与 embedded grails domain property 有关。

如@deepen 所述,此错误与嵌入式域的映射设置有关 classes。在我的例子中,我使用了多个数据库(mongodb gorm 6.0.12 + jtds(sql 服务器)),并且一些文档具有嵌入式属性(在相同的 class 中定义) .

DomainA.groovy
class DomainA
    static hasMany = [domainBs: DomainB]
    static embedded = ['domainBs']
    static mapWith = "mongo"

class DomainB
    // if embedded: what's the point of this? 
    static belongsTo = [domainB: DomainA]
    // if i'll use it as embedded, doesn't add nothing
    static mapWith = "mongo"

简而言之,如果我在嵌入class的定义中注释belongsTomapWith,问题就解决了。如果不是,必须记得mapWith嵌入class与容器class相同的数据源。