[英]JPA - How to achieve this SQL behaviour (left join parent entity to previously inner joined child & grand child entities)
I'm not being able to find out the way to achieve desired result with JPA/JPQL as I'm with this SQL query: 我无法使用JPA / JPQL来找到达到期望结果的方法,因为我正在使用此SQL查询:
select parent.id, child.id, grandchild.id
from salesforce.parent__c parent
left outer join salesforce.child__c child
inner join salesforce.grandchild grandchild on child.grandchild_id__c=grandchild.sfid
and (grandchild.token__c='....')
on parent.sfid=child.parent_id__c
where (parent.external_id__c is not null)
and parent.displayed__c=true
and parent.internal__c=false
and parent.date__c>='....'
and parent.date__c<='....'
order by parent.date__c asc
The target output of the query I'm looking for would be something like this: 我要查找的查询的目标输出将是这样的:
Parent Child Grandchild
P1 null null
P2 null null
P3 C1 GC1
P4 C2 GC2
Where all records from parent meet parent's filters, and a child record is not returned unless its grandchild meets gc's filters. 凡来自父级的所有记录均符合父级的过滤器,并且子级记录不会返回,除非其子孙符合gc的过滤器。
My best guess on JPQL would be something like this 我对JPQL的最佳猜测是这样的
SELECT DISTINCT p FROM Parent p
LEFT JOIN p.childs cl
INNER JOIN cl.grandchilds c
ON c.token__c = '....'
ON cl.parent__c = p.sfid
WHERE p.external_id__c IS NOT NULL
AND p.displayed__c = true
AND p.internal__c = false
AND p.date__c >= '....'
AND p.date__c <= '....'
ORDER BY p.date__c asc
But this is throwing org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ON
但这会抛出
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ON
The way i'm doing the query i assume is just OK: 我假设我做查询的方式就可以了:
String queryTxt = "SELECT DISTINCT p FROM Parent p LEFT JOIN p.childs cl INNER JOIN cl.grandchilds c ON c.token__c = '....' ON cl.parent__c = p.sfid WHERE p.external_id__c IS NOT NULL AND p.displayed__c = true AND p.internal__c = false AND p.date__c >= '....' AND p.date__c <= '....' ORDER BY p.date__c asc";
TypedQuery<Parent> typedQuery = entityManager.createQuery(queryTxt, Parent.class);
List<Parent> resultList = typedQuery.getResultList();
If I try with the following JPQL it executes fine: 如果我尝试使用以下JPQL,它将执行得很好:
SELECT DISTINCT p FROM Parent p
LEFT JOIN p.childs cl
INNER JOIN cl.grandchilds c
ON c.token__c = '....'
WHERE p.external_id__c IS NOT NULL
AND p.displayed__c = true
AND p.internal__c = false
AND p.date__c >= '....'
AND p.date__c <= '....'
ORDER BY p.date__c asc
But the results are not what i'm looking for, as the inner join between child and grandchild is filtering out as well parent records: 但是结果不是我想要的,因为子级和孙级之间的内部联接正在过滤掉父级记录:
Parent Child Grandchild
P3 C1 GC1
P4 C2 GC2
I've also tried with Query query = entityManager.createNativeQuery(strQuery, Parent.class);
我也尝试过使用
Query query = entityManager.createNativeQuery(strQuery, Parent.class);
using the SQL query on the beginning of the post, replacing the select part with parent. 在帖子开头使用SQL查询,将选择部分替换为父部分。 , child.
,孩子。 , grandchild.*, but then I'm not being able to parse full structure properly either :-\\
,孙子。*,但是我也无法正确解析完整结构:-\\
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.