【问题标题】:Found shared references to a collection exception in grails在 grails 中找到对集合异常的共享引用
【发布时间】:2013-02-06 23:21:37
【问题描述】:

下面的代码行有一个例外:

  def order = new PostOrder(pOrder: "post", posts: status, children: lookupPerson().children)

lookupPerson().children 给出一组 2 个具有不同 id 的子实例

完整的例外是:

  org.hibernate.HibernateException: Found shared references to a collection: com.fyp.timeline.PostOrder.children
at com.fyp.timeline.ProfileController$$ENwi3LDE.updateStatus(ProfileController.groovy:134)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)

我真的坚持这一点。主要是因为它确实曾经工作并随机停止。我已经向 MySql 和一个新数据库添加了一个新用户,但这不应该影响这个。

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    您将lookupPerson().children 传递给PostOrder 的构造函数——我假设这是Person 类中的一个集合,与static hasMany = [ ... ] 映射。所以 Hibernate 告诉你发生了什么——你使用了同一个集合两次。对我来说,映射集合应该只有一个所有者是有道理的。请记住,这些不是常规的 ArrayLists 或 HashSets - 它们是 Hibernate PersistentLists 和 PersistentSets,它们实现了正确的接口,但它们是特定于 Hibernate 的。

    如果要将集合项传递给新的PostOrder,可以将它们复制到新集合中,例如

    def order = new PostOrder(
        pOrder: "post", posts: status,
        children: [] + lookupPerson().children)
    

    但是,由于这可能也是一个映射集合,您应该使用addToChildren,例如

    def order = new PostOrder(pOrder: "post", posts: status)
    lookupPerson().children.each { order.addToChildren it }
    

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 2012-08-12
      • 2010-12-14
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 2019-10-24
      相关资源
      最近更新 更多