Reputation: 461
My problem is this; I have an entity that has 2 lists of other entities. When I use a query to select them all like this;
select DISTINCT ru FROM RtsResource ru WHERE ru.resourceId= :arg1
I get around 500 hibernate selects and it is incredibly slow. So I tried;
select DISTINCT ru FROM RtsResource ru JOIN FETCH ru.projectResources JOIN FETCH ru.resourceSkills WHERE ru.resourceId= :arg1
Which is much faster, but it only selects queries where projectResources or resourceSkills aren't null.
Is there a way to write a query similar to the second one but also including null values?
Alternatively is there a way to solve the problem with #1 without using Fetch Joins?
Worth noting I'm using Java with Spring, JPA and Hibernate.
Upvotes: 2
Views: 2187
Reputation: 461
After reading through a bunch of documentation I found out that LEFT FETCH JOIN statements were invented for this very purpose. The query should read as;
select DISTINCT ru FROM RtsResource ru LEFT JOIN FETCH ru.projectResources LEFT JOIN FETCH ru.resourceSkills WHERE ru.resourceId= :arg1
Which works perfectly.
Upvotes: 3