From a legacy system, we have 1 table (Entity) and 1 view (UserDetail) with no constraints but with relations.
Entity
| Id | Desc | Created_By |...
UserDetail
| UserId | F_Name | L_Name |...
CreatedBy has the userid of the user who created that entity. We have a unidirectional mapping in Entity.java like below.
@OneToOne
@JoinColumn(name = "CREATED_BY", referencedColumnName = "USER_ID")
private UserDetail createdBy;
Problem
But since its a legacy system, we don't have control on that and an user is hard deleted.
When the entityReposity.findAll is called, following error is thrown
org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com...UserDetail with id 92237; nested exception is javax.persistence.EntityNotFoundException: Unable to find com...UserDetail with id 92237
Question
How to make the JPA consider this association is optional or is there any way in JPA to make it ignore (like @NotFound in Hibernate)?
Tried
@NotFound works, but we need to hack @OneToOne as @ManyToOne. Also we want an implementation independent solution. Noticed this bug too, https://hibernate.atlassian.net/browse/ANN-725
optional=true - If this would have worked as per the syntactical meaning on how it should behave, I wouldn't have written this.
Also tried the postLoad() in UserDetails.java.
@PostLoad solution We have aop exceptionhandler and exception is caught there before @PostLoad in Entity class gets called.
In Entity.java
@PostLoad
public void postLoad(){
try {
System.out.println(this.getCreatedBy()); //Before control comes here, it goes to aop exception handler
}
catch (EntityNotFoundException e){
setCreatedBy(null);
}
}
Any help would be appreciated.