简体   繁体   中英

JPQL Join Fetch on a table with nullable columns

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM