在我的Play应用程序中使用Ebean / JPA,如何删除OneToOne关系中的对象? [英] Using Ebean/JPA in my Play application, how can I delete an object in a OneToOne relationship?
问题描述
我有以下课程:
import play.db.ebean.Model;
import javax.persistence.*;
@Entity
public class A extends Model {
@Id
private int id;
/* Other irrelevant properties */
@OneToOne(cascade = CascadeType.ALL, optional = true)
private B b;
}
import play.db.ebean.Model;
import javax.persistence.*;
@Entity
public class B extends Model {
@Id
private int id;
/* Other irrelevant properties */
@OneToOne(mappedBy = "b")
private A a;
@OneToOne(cascade = CascadeType.ALL, optional = false)
private C c;
}
import play.db.ebean.Model;
import javax.persistence.*;
@Entity
public class C extends Model {
@Id
private int id;
/* Other irrelevant properties */
@OneToOne(mappedBy = "c")
private B B;
}
在数据库中,它看起来像这样:
In the database, it looks like this:
Table a
Columns id, ... (other irrelevant columns), b_id
Table b
Columns id, ...(other irrelevant columns), c_id
Table c
Columns id, ...(other irrelevant columns)
现在在一个控制器方法中(对于那些熟悉Play的人)我有一个A的对象,并希望从数据库中删除它的属性b 。这也应该级联到c,所以c也会被删除。
这是我目前的做法:
Now in one the controller methods (for those familiar with Play) I have an object of A, and would like to delete its property "b" from the database. This should also cascade to c, so c gets deleted too. This is my current approach:
B b = a.getB();
b.delete();
这会引发异常:
[PersistenceException:ERROR执行DML bindLog []错误[不能
删除或更新父行:外键约束失败
(databasename
。a
,CONSTRAINTfk_a_payme_4
FOREIGN KEY(b_id
)
REFERENCESb
(id
))]]
[PersistenceException: ERROR executing DML bindLog[] error[Cannot delete or update a parent row: a foreign key constraint fails (
databasename
.a
, CONSTRAINTfk_a_payme_4
FOREIGN KEY (b_id
) REFERENCESb
(id
))]]
它基本上归结为我正在尝试删除b,而a仍然在b_id列中保存对b的外键引用,所以我应该首先设置引用null。
It basically boils down to the fact that I'm trying to delete b, while a still holds a foreign key reference to b in column b_id, so I should first set that reference to null.
在Java中,这转换为:
In Java, this translates to:
B b = a.getB();
a.setB(null);
a.update();
b.delete();
这会将对象a中b的引用设置为null并正确删除b对象,但是c不会从数据库中删除。 (为什么?我虽然cascade属性会照顾这个)
我发现修复此问题的唯一方法是明确删除c,所以这样:
This does set the reference to b in object a to null and correctly deletes the b object, but c does not get deleted from the database. (Why? I though the cascade property would take care of this) The only way I have found to fix this is to explicitly delete c as well, so like this:
B b = a.getB();
C c = b.getC();
b.setC(null);
b.update();
c.delete();
a.setB(null);
a.update();
b.delete();
我对此并不十分满意,因为已经有8行代码删除了两行在数据库中,如果这张照片中有更多的关系会更多。
I'm not very pleased about this though, because that's already 8 lines of code to delete two rows in the database, and it would be more if there were more relationships in this picture.
至于我的问题:
如何从a中删除b,从而首先自动删除a到b的引用,如何在删除b时确保删除c?
提前感谢您!
编辑:目前为止最好的想法:将ab关系的所有权移至b。
best idea so far: move the ownership of the a-b relationship to b.
推荐答案
尝试使用
class A{
......
@JoinColumn(nullable=true)
private B b;
.......
}
参见有关详细信息,请转至此 @ManyToOne(可选= false)与@Column(nullable = false)之间的区别。
See this question for more info What is the difference between @ManyToOne(optional=false) vs. @Column(nullable=false).
这篇关于在我的Play应用程序中使用Ebean / JPA,如何删除OneToOne关系中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!