I have this inheriance tree
Code:
public class Entry {
...
private Long id;
private String field1;
...getters and setters...
}
public class InEntry extends Entry {
...
private List<OutEntry> outEntries;
...getters and setters...
}
public class OutEntry extends Entry {
...
private InEntry inEntry;
...getters and setters...
}
I have a JPQL that fetches a List<Entry> based on some criteria, the simplest form, i.e. no criteria at all, looks like:
Code:
from Entry e
where e.id in (
select ie.id
from InEntry ie
left outer join ie.outEntries oe
where oe.field1 is null
) or e.id in (
select oe.id
from InEntry ie
left outer join ie.outEntries oe
where oe.field1 is not null
)
Once I get this list, I am putting the data into a DTO, at which stage, if what i have is an instance of OutEntry, I call ((OutEntry)entry).getInEntry().
This results in an N+1 select. I want to optimize my code to avoid that N+1 select. I'm using Hibernate+JPA+Spring. I use JPA Annotations, but I'm willing to use Hibernate annotations if that can solve my problem.
All my joins are using a LAZY fetch.
Is there a way I can do this using annotations, or JPQL, or HQL?