We are using JPA to load some stuff from a database. Some entities may have optional relationships between them, e.g.
@Entity
public class First {
....
@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumns(value = {
JoinColumn(name = "A_ID", referencedColumnName = "A_ID", insertable = false, updatable = false),
JoinColumn(name = "B_ID", referencedColumnName = "B_ID", insertable = false, updatable = false)})
private Second second;
When this association is present in the database, everything is working fine. When it's not, I'm getting a javax.persistence.EntityNotFoundException
What I want is instead of the exception to have this field as NULL if the association is not present.
I have tried several different things, e.g. using the optional=true in the relationship annotation (which btw is the default option), putting it as Nullable, etc. Nothing seems to do the trick, it seems like all these options are being ignored.
I found a lot of links mentioning this very same problem (and some questions here in stackoverflow) but in all of them the suggestion is to use the @NotFound
annotation from Hibernate. But we do NOT want to have any dependencies to Hibernate (we want to keep everything pure JPA).
Does any of you guys know any other way to solve this?
Many thanks for all your help!