删除一对一关系
我有这些课程。
@Entity
@Table(name ="a")
class A{
private Integer aId;
private B b;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_a")
public Integer getAId() {
return aId;
}
@OneToOne(mappedBy = "a", cascade={CascadeType.ALL})
public B getB() {
return b;
}
}
@Entity
@Table (name= "b")
class B{
private A a;
@OneToOne
@JoinColumn(name = "id_a")
public A getA() {
return a;
}
}
表格看起来像:
A) | id_A |...其他字段...|
B) | id_B | fk_id_A |..其他字段..|
但是,当我尝试删除 A 的实例时,我得到,
org.hibernate.ObjectDeletedException: 删除的对象将被重新保存 级联: (从关联中删除已删除的对象)[B#130]
我尝试在交叉引用上设置 null:
b.setA(null)
a.setB(null)
但仍然会引发异常。
我所完成的就是删除 A 中的一行,并将 B 的外键保留为空,但是当我重新尝试删除 B 时,得到相同的错误。
这是我的删除代码:
public static void delete(Object object) throws Exception {
Transaction tx = getSession().beginTransaction();
try {
getSession().delete(object);
tx.commit();
} catch (Exception ex) {
tx.rollback();
getSession().close();
throw ex;
}
}
getSession
始终返回有效的会话。
我有什么遗漏的吗?
I have these classes.
@Entity
@Table(name ="a")
class A{
private Integer aId;
private B b;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_a")
public Integer getAId() {
return aId;
}
@OneToOne(mappedBy = "a", cascade={CascadeType.ALL})
public B getB() {
return b;
}
}
@Entity
@Table (name= "b")
class B{
private A a;
@OneToOne
@JoinColumn(name = "id_a")
public A getA() {
return a;
}
}
And tables look like:
A) | id_A |....other fields...|
B) | id_B | fk_id_A |..other fields..|
But, when I try to delete an instance of A, I get,
org.hibernate.ObjectDeletedException:
deleted object would be re-saved by
cascade :
(remove deleted object from associations)[B#130]
I've tried setting null on cross references:
b.setA(null)
a.setB(null)
But exception still gets thrown.
All that I have accomplished is to delete a row in A, and leave B's foreign Key null, but when i re try to delete B, get the same error.
This is my delete code:
public static void delete(Object object) throws Exception {
Transaction tx = getSession().beginTransaction();
try {
getSession().delete(object);
tx.commit();
} catch (Exception ex) {
tx.rollback();
getSession().close();
throw ex;
}
}
getSession
always returns a valid session.
Is there anything I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从顶部开始并继续向下工作。 您需要删除表 B 中对 A 的引用。因此,在 A 中找到您要删除的记录在 B 中的引用,然后删除 B 中的该记录。然后返回到 A 并删除 A 中的记录。
如果您如果使用 SQL Server,您可以在表 A 上设置级联删除,这将自动为您完成。 不过你必须小心这一点。 对于复杂的结构,我不推荐这样做。
Start from top and continue working down. You need to delete the reference to A in table B. So locate the reference in B of the record you're trying to delete in A and delete that record in B. Then go back to A and delete the record in A.
If you're using SQL Server, you can set cascading delete on table A and this will be done for you automatically. You have to be careful with this though. I wouldn't recommend this for complex structures.
您是否在单独的事务中执行交叉引用的清零? 可能需要提交这些更改才能使删除生效。
Are you performing the nulling of cross-references in a separate transaction? Those changes may need to be committed for the delete to work.