【问题标题】:Grails withNewSession does not flushGrails withNewSession 不刷新
【发布时间】:2012-10-05 16:47:28
【问题描述】:

在一个 grails 域中,a 已经实现了beforeDelete,如下所示

class Shop {
    def beforeDelete() {
        Shop.withNewSession {
            Client.findAllByShop(this)*.shop = null                 
        }    
    }
}

但是client shop null值没有持久化到数据库中。

如果我添加手动会话刷新

class Shop {
    def beforeDelete() {
        Shop.withNewSession { s2->
            Client.findAllByShop(this)*.shop = null         
            s2.flush()  
            s2.clear()
        }
    }
}

它有效,客户商店的价值在数据库中为空。

这是一个 Grails 错误还是我误解了文档? withNewSession 不是暗示自动刷新吗?

【问题讨论】:

    标签: hibernate grails grails-orm hibernate-session


    【解决方案1】:

    文档(向下滚动到beforeDelete 示例here)似乎暗示不需要刷新或清除会话。

    Burt Beckwith 还在 Grails 邮件列表(参见线程 here)上指出,在 withNewSession 闭包中不需要手动调用 flush()clear()

    话虽如此,从 Grails 2.2.1 开始,似乎确实存在使用 withNewSession 的错误报告(请参阅详细信息 here)。

    【讨论】:

      【解决方案2】:

      withNewSession 为您提供一个新的 Hibernate 会话,但它不一定是事务性的。听起来您想使用withTransaction 而不是withNewSession

      【讨论】:

      • 所以你建议这样? def beforeDelete(){ Shop.withNewSession { Shop.withTransaction{ Client.findAllByShop(this)*.shop = null } } }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      相关资源
      最近更新 更多