更改 JPA OneToOne 关系中的项目后如何删除行?
如何使用 JPA/Hibernate 自动删除 OneToOne 项目? 我希望简单地在包含的类中将 OneToOne 项设置为 null 就足以允许 Hibernate 删除它。
给定一个简单的对象,经过简化:
@Entity
public class Container {
private Item item;
@OneToOne(cascade=CascadeType.ALL)
public Item getItem() { return item; }
public void setItem(Item newItem) { item = newItem; }
}
当在容器上设置一个项目时,容器将通过合并来持久化,并插入一行。
Container container = new Container();
container.setItem(new Item());
container = entityManager.merge(container);
// Row count is 1
但是当该项目设置为 null 或设置为另一个项目时,旧对象仍然存在于表中。
container.setItem(null);
container = entityManager.merge(container);
// Row count is STILL 1, leaving orphaned rows.
那么,如何删除这些 OneToOne 孤儿呢?
How do you get a OneToOne item to automatically remove with JPA/Hibernate? I would expect simply setting the OneToOne item to be null in the class that contains would be smart enough to allow Hibernate to delete it.
Given a simple object, simplified:
@Entity
public class Container {
private Item item;
@OneToOne(cascade=CascadeType.ALL)
public Item getItem() { return item; }
public void setItem(Item newItem) { item = newItem; }
}
When an Item is set on Container an Container is persisted with merge a row gets inserted.
Container container = new Container();
container.setItem(new Item());
container = entityManager.merge(container);
// Row count is 1
But when the item is set null, or to another item, the old object still exists in the table.
container.setItem(null);
container = entityManager.merge(container);
// Row count is STILL 1, leaving orphaned rows.
So, how do I remove these OneToOne orphans?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜测 hibernate 不允许
DELETE_ORPHAN
到OneToOne
关系的原因与 此问题。如果您真的想要这样,您可以通过以下步骤破解您的方式:
当然,这是一个很大的技巧。
I'm guessing that the reasone behind hibernate not allowing
DELETE_ORPHAN
toOneToOne
relations is related to this issue.If you really want this bad, you can hack your way with these steps:
Of course, this is a big hack.
由于 JPA 2.0 已经发布很长时间了,您可以简单地使用:
As JPA 2.0 has been released for a very long time now, you could simply use:
尝试更改为
另请参阅我对类似帖子的回答 这里。
Try to change to
See also my answer on similar post here.
不幸的是,如果不将自己与 Hibernate 的实现联系起来,就无法在 JPA 中做到这一点。
所以,是的,正如 Foxy 所说,您可以使用 org.hibernate.annotations.CascadeType 而不是标准 JPA 注释,它允许您指定 DELETE_ORPHAN 。 如果您想使用 JPA 抽象,您必须自己删除孤儿。
Unfortunately, there is no way to do this in JPA without tying yourself to Hibernate's implementation.
So yes, as Foxy says, you can use
org.hibernate.annotations.CascadeType
instead of the standard JPA annotation, which allows you to specifyDELETE_ORPHAN
. If you want to use the JPA abstraction, you must delete orphans yourself as of yet.