简体   繁体   English

在具有可空列的表上进行JPQL Join Fetch

[英]JPQL Join Fetch on a table with nullable columns

My problem is this; 我的问题是 I have an entity that has 2 lists of other entities. 我有一个具有2个其他实体列表的实体。 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. 我大约有500个休眠选择,而且速度非常慢。 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. 这要快得多,但是它只选择projectResources或resourceSkills不为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? 另外,是否有一种方法可以解决#1的问题而无需使用提取联接?

Worth noting I'm using Java with Spring, JPA and Hibernate. 值得注意的是,我将Java与Spring,JPA和Hibernate结合使用。

After reading through a bunch of documentation I found out that LEFT FETCH JOIN statements were invented for this very purpose. 阅读一堆文档后,我发现LEFT FETCH JOIN语句就是为此目的而发明的。 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. 哪个完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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