JPA/Hibernate 删除实体有时不起作用 [英] JPA/Hibernate remove entity sometimes not working

查看:32
本文介绍了JPA/Hibernate 删除实体有时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下通常运行良好的代码:

I have the following code that usually works well:

public void delete(T object)
{
  EntityManager em = getPersistence().createEntityManager();
  EntityTransaction et = em.getTransaction();
  try
  {
    et.begin();
    object = em.find(object.getClass(), object.getId());
    em.remove(object);
    em.flush();
    et.commit();
  }
  catch(Exception e)
  {
    error("Unable to delete " + object.toString() + ": there are references to it.");
  }
  finally
  {
    if (et.isActive()) et.rollback();
    em.close();
  }
}

对于我的许多实体类,这很有效.但是对于其中两个它什么都不做,它不会抛出任何异常并且它不会删除该对象.来自 hibernate 的日志显示 hibernate 执行了许多选择查询,但它甚至没有尝试执行删除.

For many of my entity classes this just works. However for two of them it does nothing, it does not throw any exceptions and it does not delete the object. The log from hibernate shows that hibernate executes a number of select queries but it doesn't even try to execute a delete.

我已经尝试过其他类似问题中的建议这里此处,但无济于事(好吧,后者建议 @Transactional 我不能使用,但我只是将语句括在 begin()commit() 之间.

I've already tried suggestions found in other similar questions here and here, but to no avail (well, the latter suggests @Transactional which I can't use, but I just enclosed the statements between begin() and commit() instead).

我似乎无法找到这两个班级比其他班级多(或少)的地方.他们使用 @PrimaryKeyJoinColumn 就像我拥有的​​几乎所有其他实体一样,他们有 @OneToMany@ManyToOne 就像 ohters.老实说,他们确实有一个 @OneToOne(optional = false) 字段,该字段引用另一个类而其他实体没有,但我不会经历更改它的麻烦(因此更改数据库架构),除非您告诉我可能有原因.

I can't seem to find what those two classes have more (or less) than the others. They use @PrimaryKeyJoinColumn just like almost all other entities I have, they have @OneToMany and @ManyToOne just like ohters. To be honest, they do have a @OneToOne(optional = false) field that references another class and that other entities do not have, but I wouldn't go through the hassle of changing that (and consequently changing the database schema) unless you tell me there could be a reason for it.

@OneToOne 负责吗?还是我的删除代码被窃听了?

Is @OneToOne responsible? Or is my delete code bugged?

推荐答案

你在这个图中是否有关联级联到被删除的事物?如果是这样,JPA 规范明确指出提供者将在这种情况下取​​消删除.如果是这种情况,Hibernate 会写出一条日志语句,说明取消调度实体删除 [...]".您可以通过在 org.hibernate.event.internal.DefaultPersistEventListener 记录器上启用跟踪日志记录来看到这一点.

Do you have associations in this graph that cascade a persist back to the thing being deleted? If so, the JPA spec states clearly that the provider is to cancel the delete in such a case. If this is the case, Hibernate writes out a log statement saying "un-scheduling entity deletion [...]". You could see that by enabling trace logging on the org.hibernate.event.internal.DefaultPersistEventListener logger.

如果是这种情况,您需要按照 JPA 规范的要求清理这些关联.

If this is the situation, you'll need to clean up those associations as required by JPA specification.

这篇关于JPA/Hibernate 删除实体有时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆