JPQL Join Fetch on a table with nullable columns

半世苍凉 提交于 2019-12-11 05:23:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/28694929/jpql-join-fetch-on-a-table-with-nullable-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!