我找到了解决方案。我切换了这个映射的方向。所以我mappedBy = "a"
从A
课堂上删除并mappedBy = "b"
在B
课堂上添加。
所以代码现在看起来是这样的:
@Entity
public class A extends Model {
@Id
private Long id;
@OneToOne(optional = true, cascade = CascadeType.REMOVE, orphanRemoval = true)
private B b;
...
}
@Entity
public class B extends Model {
@Id
private Long id;
@OneToOne(optional = false, mappedBy = "b")
private A a;
private String name;
...
}
我name
在 B 类中添加了字段以使这个测试更有趣。
我的测试方法:
@Test
public void abTest () {
FakeApplication app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
Helpers.start(app);
A a = new A();
B b = new B();
a.setId(1L);
b.setId(2L);
a.setB(b);
b.setA(a);
b.setName("bbb");
Ebean.save(b);
Ebean.save(a);
A fa = Ebean.find(A.class, 1L);
System.out.println("a.id: "+fa.getId());
System.out.println("a.b.id: "+fa.getB());
System.out.println("a.b.name: "+fa.getB().getName());
A a1 = new A();
a1.setId(3L);
Ebean.save(a1);
A fa1 = Ebean.find(A.class, 3L);
System.out.println("a1.id: "+fa1.getId());
System.out.println("a1.b.id: "+fa1.getB());
B fb = Ebean.find(B.class, 2L);
System.out.println("b.id: "+fb.getId());
System.out.println("b.name: "+fb.getName());
System.out.println("b.a.id: "+fb.getA().getId());
}
而这个测试的结果是:
[debug] c.j.b.PreparedStatementHandle - insert into b (id, name) values (2,'bbb')
[debug] c.j.b.PreparedStatementHandle - insert into a (id, b_id) values (1,2)
[debug] c.j.b.PreparedStatementHandle - select t0.id c0, t0.b_id c1 from a t0 where t0.id = 1
a.id: 1
a.b.id: models.B@2
[debug] c.j.b.PreparedStatementHandle - select t0.id c0, t0.name c1, t1.id c2 from b t0 left outer join a t1 on t1.b_id = t0.id where t0.id = 2
a.b.name: bbb
[debug] c.j.b.PreparedStatementHandle - insert into a (id, b_id) values (3,'[SQL NULL of type -5]')
[debug] c.j.b.PreparedStatementHandle - select t0.id c0, t0.b_id c1 from a t0 where t0.id = 3
a1.id: 3
a1.b.id: null
[debug] c.j.b.PreparedStatementHandle - select t0.id c0, t0.name c1, t1.id c2 from b t0 left outer join a t1 on t1.b_id = t0.id where t0.id = 2
b.id: 2
b.name: bbb
b.a.id: 1
因此,无论是否有效,此代码A.b
都可以正常工作null
。正如我们在日志中看到的那样,现在left outer join
有join
.