This is related to this question, but the example below is shorter, so I figured another question on this would make sense.
I have two entities, A and B, in a one-to-one relationship. For an A, a B is optional, and every B must have an A. I want to cascade deletes from A to B. Here's my first attempt:
@Entity
public class A extends Model {
@Id
private Long id;
@OneToOne(optional = true, mappedBy = "a", cascade = CascadeType.REMOVE, orphanRemoval = true)
private B b;
}
@Entity
public class B extends Model {
@Id
private Long id;
@OneToOne(optional = false)
private A a;
}
However, it seems like Ebean ignores the "optional" annotation, because when I execute a find for a saved A with id 1, the following SQL is executed:
select t0.id c0, t1.id c1
from a t0
join b t1 on t1.a_id = t0.id
where t0.id = 1
In other words, it does an inner instead of a left join, which causes the find to fail when there's no associated B. I've tried various combinations of @JoinColumn
, etc. to no avail. The only somewhat satisfactory workaround I've found is to model A-to-B as a "fake" one-to-many relationship. Is there a better solution? Is this a bug or is it a known/stated limitation of Ebean?