0
votes

QueryDSL is not working properly with JPA for left join. I am using queryDSL version 4.2.1 and mapping the response directly to javax.persistence entity. For Left join/Right join, the joining condition it's not working. It fetches every entity disregarding the joining condition(here it's name = "testName") when entity1.getEntity2() is being called.

Is there any other way applicable for this case to map the result after JOIN tables ?

JPAQuery<Entity1> query = new JPAQuery<>(entityManager);
query.from(table1);
query.leftJoin(table2).on(table2.id.eq(table1.id).and(table2.name.eq("testName"));
List<Entity1> list = query.fetch();
@Entity
public class Entity1{

 private Integer id;

 @OneToMany(mappedBy = "entity1", fetch = FetchType.LAZY)
 private List<Entity2> entity2;
}
1

1 Answers

0
votes

An left or right join is an outer join.

So if you use left join all records from the left (in your case table1) will be selected.

If you only want records from table1 if there are corresponding records on table2 you have to use innerJoin.

JPAQuery<Entity1> query = new JPAQuery<>(entityManager);
query.from(table1);
query.innerJoin(table2).on(table2.id.eq(table1.id).and(table2.name.eq("testName"));
List<Entity1> list = query.fetch();

Read more about the join types here:

https://www.diffen.com/difference/Inner_Join_vs_Outer_Join