简体   繁体   English

左边连接spring数据jpa和querydsl

[英]left join with spring data jpa and querydsl

I am using spring data jpa and querydsl and trapped on how to write simple nice query to left join two tables. 我正在使用spring数据jpa和querydsl并且被困在如何编写简单的好查询到左连接两个表。 Suppose I have an Project entity and a Task entity with OneToMany relationship defined in Project, I would like to do something like: 假设我有一个Project实体和一个在Project中定义了OneToMany关系的Task实体,我想做的事情如下:

select * from project p left join task t on p.id = t.project_id where p.id = searchTerm
select * from project p left join task t on p.id = t.project_id where t.taskname = searchTerm

In JPQL, it should be: 在JPQL中,它应该是:

select distinct p from Project p left join p.tasks t where t.projectID = searthTerm
select distinct p from Project p left join p.tasks t where t.taskName = searthTerm

I have a ProjectRepository interface, which extends JpaRepository and QueryDslPredicateExecutor. 我有一个ProjectRepository接口,它扩展了JpaRepository和QueryDslPredicateExecutor。 That gives me access to method: 这让我可以访问方法:

Page<T> findAll(com.mysema.query.types.Predicate predicate, Pageable pageable) 

I know that left join can be easily achieved by creating a new JPAQuery(entityManager). 我知道通过创建一个新的JPAQuery(entityManager)可以很容易地实现左连接。 But I do not have entity manager explicitly injected with spring data jpa. 但我没有明确注入spring数据jpa的实体管理器。 Is there nice and simple way to build a predicate with left join? 使用左连接构建谓词是否有简单明了的方法? Wish someone here have experienced this and is able to give me an example. 希望有人在这里体验过这一点,并且能够给我一个例子。 Thank you. 谢谢。

Frey. 弗雷。

If you want to express a constraint on tasks then you can do it like this 如果你想表达对任务的约束,那么你可以这样做

QProject.project.tasks.any().id.eq(searchTerm)

If you want to express preloading of certain tasks instead via a left join you can't express that via a Predicate. 如果您想通过左连接来表达某些任务的预加载,则无法通过谓词来表达。 A Predicate in Querydsl is a boolean expression for the where, join-on and having parts of the query. Querydsl中的谓词是一个布尔表达式,用于where,join-on和包含查询的部分。

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

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