【问题标题】:Grails and 3rd party tool use same database: how to update grails after 3rd party deletes?Grails 和 3rd 方工具使用相同的数据库:如何在 3rd 方删除后更新 grails?
【发布时间】:2017-04-10 16:45:39
【问题描述】:

我正在编写一个服务方法 (Grails 2.5.5),它使用第 3 方休息 API 删除实体。在我的方法的中途,我在其中一个实体上调用.refresh(),我得到了这个异常:

org.springframework.orm.hibernate4.HibernateObjectRetrievalFailureException: No row with the given identifier exists: [MyEntity#7103]; nested exception is org.hibernate.UnresolvableObjectException: No row with the given identifier exists: [MyEntity#7103]

这是真的,因为我刚刚使用 REST API 删除了它。如果重要,已删除的 MyEntity 位于我要刷新的实体上的 hasMany 中。我不(想我)想将此标记为ignoreNotFound

我如何告诉 Grails/Hibernate 这条记录不再存在也没关系?

【问题讨论】:

  • 哈哈,我刚刚又遇到了这个问题,有一段时间很兴奋,因为 Google 找到了对我来说如此有用的 SO 问题!

标签: hibernate grails


【解决方案1】:

我遇到了同样的问题并找到了下一个解决方案。

所以你有实体 E 有实体 A 的列表。 如果您需要删除实体 A,您需要执行以下操作:

 public void deleteA(List<Integer> ids) {
    if (!ids) {
        return
    }
    List<A> aToDelete = A.findAllByIdInList(ids)
    aToDelete.each { A a ->
        if(a.e) {
            E e = a.e
            e.a.remove(a)
        } else {
            a.delete()
        }
    }
}

所以想法是检查 A 是否与 E 有任何关系,以防是通过 E 删除它,否则直接删除它。对我来说效果很好。

【讨论】:

  • 第 3 方工具和我的 grails webapp 都查看同一个数据库。如果我使用 3rd 方工具的 REST API 删除实体(通常涉及删除多个表中的行),那么如何通知 grails 删除了什么?
猜你喜欢
  • 2014-07-05
  • 1970-01-01
  • 2019-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多