How to implement left or right outer join in QueryDsl

2,149 views
Skip to first unread message

George Li

unread,
Oct 7, 2016, 1:40:33 PM10/7/16
to Querydsl
1) I have a simple query:

     JPAQuery query = queryUtil.createJpaQuery().from(parent).leftJoin(parent.child, child). 

and like to show all records in parent even no child. How to implement this outer join.

2) If 2 tables have no relationships, is it possible to join in QueryDsl?

Thanks
George

Richard Richter

unread,
Oct 8, 2016, 7:02:41 AM10/8/16
to Querydsl
Hi


1) I have a simple query:
     JPAQuery query = queryUtil.createJpaQuery().from(parent).leftJoin(parent.child, child). 
and like to show all records in parent even no child. How to implement this outer join.

I don't understand the problem - you used left join and that will preserve all the parents even if no child is there.
 
2) If 2 tables have no relationships, is it possible to join in QueryDsl?

Yes it is, although it is not technically pure JPA (both EclipseLink and Hibernate (above 5.1) support it):

List<Dog> dogs = new JPAQuery<Dog>(em)
  .select(QDog.dog)
  .from(QDog.dog)
  .leftJoin(QBreed.breed).on(QBreed.breed.id.eq(QDog.dog.breedId))
  .where(QBreed.breed.name.contains("ll"))
  .fetch();

The only non-JPA problem here is "...Join(QBreed.breed)" - that is join on the root entity. JPA specification only allows "association paths" that is QDog.dog.breed - and that means mapping the relationship. But if you dare to go beyond specification you will get what you want with all current JPA 2.1 providers supporting it.

WARNING: Works on JPA 2.1, not before, because previous JPA versions don't have ON clause that is also crucial in this.

Cheers

Virgo
Reply all
Reply to author
Forward
0 new messages