简体   繁体   English

JPA-如何实现此SQL行为(左联接父实体到先前内部联接的子实体和大子实体)

[英]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 :-\\ ,孙子。*,但是我也无法正确解析完整结构:-\\

You can do any query with FluentJPA and use its Tuple support for projections. 您可以使用FluentJPA进行任何查询,并将其Tuple支持用于投影。 Provide me your entities and I will draft you the full solution. 提供我您的实体,我将为您起草完整的解决方案。

暂无
暂无

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

相关问题 通过JPA联接从父实体返回子实体 - Returning child entities from a parent entity with JPA join 如何使用 Lombok 在 Spring/JPA/Hibernate 中获取带有所有子实体和子实体的父实体 - How to get parent entity with all child entities and child entities of children in Spring/JPA/Hibernate with Lombok 如何使用join fetch通过一个查询获取父实体及其子实体及其子实体 - How to fetch Parent Entity with its Child Entities and their Child Entities with one query with join fetch 无法在Spring Data JPA中使用内部联接获取子实体 - Not able to fetch child entities using inner join in spring data jpa Spring Data JPA Repository with Hibernate - persist(sql insert)父实体,但只更新嵌套的子实体 - Spring Data JPA Repository with Hibernate - persist (sql insert) parent entity but only update nested child entities 使两个不同的父实体通过JPA中的@OneToMany引用子实体 - Make 2 different Parent Entities reference a Child Entity through @OneToMany in JPA JPA + Hibernate-父子表之间的内部联接 - JPA + Hibernate - Inner Join between parent and child tables 如何使用 Spring JPA DATA 仅获取特定的子实体及其具有特定值的父实体 - How to fetch only the specific child entity along with its Parent entities with specific value using Spring JPA DATA 使用 JPA Criteria,如何在不获取已加入实体的情况下获取已加入实体的子实体? - With JPA Criteria, how would I Fetch a child entity of a Joined entity without Fetching the Joined entity? 如何在JPA /休眠继承策略Joined中删除父级和子级元素? - How to remove Parent and Child Element in JPA/hibernate inheritance strategy Joined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM