I have the folowing JPA query:
SELECT emp FROM Employee emp LEFT JOIN FETCH emp.supervisor WHERE emp.supervisor_id = :id_number
Employee data is structured as follows:
employee_ id, first_name, last_name, supervisor_id
1, jeff, jones, 2
2, bob, smith, 3
etc...
where supervisor id is recursive to id of the same table. The problem is ocassionally I'll get
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
on the following EL call
#{employee.supervisor.last_name}
because the data I am using, which is read-only, will have anomalies such as the employee has a supervisor_id on their record, but that employee_id has yet to have been inserted into the system or has been filtered out of the Oracle View that I am allowed to attached my Employee Entity to.
employee_ id, first_name, last_name, supervisor_id
4, jane, dean, 9999
where employee_id 9999 doesn't exist in my read-only data feed
what I really want is to still grab the Employee Entity but just populate a Null or Empty Supervisor.
Originally I had annotated my Employee Entity with @NotFound(action = NotFoundAction.IGNORE)
on the supervisor field but that caused the Entity Manager to generate a ton of SQL call degregating the applications performance. JPA/Hibernate Query Slow with too many queries
@Entity
Employee
@OneToOne(fetch = FetchType.LAZY)
//Removed @NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "supervisor_id", insertable = false, updatable = false)
private Employee supervisor;
Am I missing some constructor on my Employee entity which will populate an empty Supervisor or is there some modification to my query which will perform the IGNORE
as the annotation did, without a ton of individual SQL calls?