我正在使用spring数据jpa和querydsl并且被困在如何编写简单的好查询到左连接两个表.假设我有一个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
Run Code Online (Sandbox Code Playgroud)
在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
Run Code Online (Sandbox Code Playgroud)
我有一个ProjectRepository接口,它扩展了JpaRepository和QueryDslPredicateExecutor.这让我可以访问方法:
Page<T> findAll(com.mysema.query.types.Predicate predicate, Pageable pageable)
Run Code Online (Sandbox Code Playgroud)
我知道,左连接可以通过创建一个新的JPAQuery(EntityManager的)可以轻松实现.但我没有实体管理器与弹簧数据的JPA明确注入.有没有建立一个谓词左加入很好的和简单的方法?希望有人在这里经历了这一点,并能够给我一个例子.谢谢.
弗雷.