【问题标题】:Upgrade issue to Grails 2.4.4升级到 Grails 2.4.4 的问题
【发布时间】:2014-12-28 06:18:19
【问题描述】:

从 Grails 2.4.3 升级到 2.4.4 后,我在启动 Grails 应用程序时不断收到错误消息。完整的错误可以在这里阅读:http://pastebin.com/UXQ34JKD

2014-10-31 16:26:32 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.List
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 org.hibernate.MappingException: Association references unmapped class: java.util.List
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
Caused by: 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.List
        ... 4 more
Caused by: 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.List
        ... 4 more
Caused by: org.hibernate.MappingException: Association references unmapped class: java.util.List
        ... 4 more

显示Association references unmapped class: java.util.List

它没有说明什么域类或任何真正有用的信息。我已经尝试删除一些域类并试图找出原因是什么,但它是一个大型应用程序并且我没有成功。

谁能指出我正确的方向来确定触发的位置以及如何解决它?

【问题讨论】:

  • 我已经在旧版本的 Grails 中看到了这个错误...

标签: grails grails-2.4


【解决方案1】:

找出问题所在

您需要做的第一件事是找出导致问题的字段。很可能它是声明为List 的域类中的任何字段

就我而言,很容易找到它们,因为该项目处于非常早期的阶段,并且没有太多的域。

但是,我找到了possible solution 来缩小可能的罪魁祸首。

有趣的是:

找出它们在哪里的唯一好方法是在 AbstractGrailsDomainBinder 的第 436 行设置断点并查看事务状态

解决问题

当您发现不正确的字段时,是时候实施解决方法了。

让我们假设我们的罪魁祸首是 List authors 在一个域类中,例如:

class Book {
   List<Integer> authors // keeps author ids
} 

当然,我们需要去掉这个列表,所以解决方案是这样的:

class Book {

    static transients = ['authors']

    String authorIds

    public void setAuthors(List<Integer> authorList) {
        this.authorIds = authorList ? authorList.join(";") : ''
    }

    public List<Integer> getAuthors() {
        return authorIds?.split(";")?.collect { it.toInteger() } ?: []
    }

} 

可能的副作用

我注意到 setter 需要显式调用才能工作。

我很确定在以前的 Grails 版本中,以下代码会调用 setter:

new Book(authors: [1, 2, 3])

但看起来在 Grails 2.4.4 中需要这样做:

def book = new Book()
book.setAuthors([1, 2, 3])

【讨论】:

  • 好吧,我真的不认为这是一种解决方法。我只是不认为使用List 作为域类中的字段是一个好习惯。但我当然愿意接受任何建议。请提供你的。
  • 如果 Grails 说它支持原始类型列表,为什么这是一种解决方法? grails.github.io/grails-doc/latest/guide/…
  • 好吧,Grails 提供了一些在现实世界中实际上并不是最好的功能,例如。 hasMany(因为性能)或将请求参数直接绑定到域对象(因为安全性)。它受支持的事实并不意味着它是最佳实践......
【解决方案2】:

尝试用真正的类替换有问题的List接口,例如ArrayList

【讨论】:

    【解决方案3】:

    您还需要指定 hasMany 映射。似乎只添加List&lt;DomainClassName&gt; listName 还不够 在您的域类

    你还需要添加

    static hasMany = [
            listName: DomainClassName
    ]
    

    至少它对我有用。

    【讨论】:

      猜你喜欢
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多