grails:org.hibernate.MappingException:关联引用未映射class:java.util.Set

grails : org.hibernate.MappingException: Association references unmapped class: java.util.Set

有人熟悉以下错误吗? 我正在使用 hibernate 4+ 和 Grails 2.4.4

这是完整的错误日志:

|Loading Grails 2.4.4
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
...........
|Compiling 1 source files
.........................
|Running Grails application
Error |
2015-03-19 16:54:11,496 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener  - 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 '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: java.util.Set
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 '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: java.util.Set
    Line | Method
->>  266 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    617 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by 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 org.hibernate.MappingException: Association references unmapped class: java.util.Set
->>  266 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    617 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Association references unmapped class: java.util.Set
->>  266 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    617 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by MappingException: Association references unmapped class: java.util.Set
->>  266 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    617 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Error |
Forked Grails VM exited with error

问题:

  • 我的一个域 类 在不应该包含大写字母的地方( 'Exploits' 而不是 'exploits' )。

解决方案:

旧代码:

package core

class Scenario {
    String name

    static belongsTo = [ Exploit ]

    static hasMany = [ Exploits : Exploit ]

    static constraints = {
        name nullable: false , maxSize: 32
    }
}

新代码:

package core

class Scenario {
    String name

    static belongsTo = [ Exploit ]

    static hasMany = [ exploits : Exploit ]

    static constraints = {
        name nullable: false , maxSize: 32
    }
}

有效!!

如果您在嵌入对象中使用像 hasMany 这样的集合,也可能会遇到此错误。

复合元素不能包含嵌套集合。

"Composite Elements" 与@Embeddable 对象相同。因此,@Embeddable 对象的集合不可能保存集合。它们必须被提升为@Entity 对象。

参考this link

将我的项目从 Grails 2.3.11 升级到 2.5.4 后,我遇到了同样的问题。事实证明,必须更新的是 spring-安全生成的 类 用户、角色和用户角色。他们有过时的 getter 和 setter,现在被解释为 bean 属性。

我遇到了类似的异常,但命名是正确的。

问题是两个数据源使用 - 用于读取和写入副本。

问题的修复添加了以下内容:

static mapping = {
    datasource 'ALL'
}