Downcast entities in a join

293 views
Skip to first unread message

Frans

unread,
Aug 29, 2019, 1:38:25 PM8/29/19
to Querydsl
Hi,

Basically a master entity (Project) that has a one to many relation with other entities that are defined with joined inheritance (Employee <- FullTimeEmployee, PartTimeEmployee, ContractEmployee)
I'm trying to build a JPA query equivalent to:

SELECT DISTINCT p FROM Project p JOIN TREAT(p.employees AS FullTimeEmployee) e
WHERE e.annualSalary > 100000

I tried with the com.querydsl.jpa.
JPQLOps.TREAT operator but I can't find a way to make a join based on that.

Any idea?

Thanks!

Luis Fernando Planella Gonzalez

unread,
Aug 30, 2019, 6:57:46 AM8/30/19
to Querydsl
You can do it like this :

EntityManager em = ...;
QProject p = QProject.project;
QFullTimeEmployee e = QFullTimeEmployee.fullTimeEmployee;
List<FullTimeEmployee> emps = new JPAQuery<>(em)
   
.select(p)
   
.distinct()
   
.from(p)
   
.innerJoin(p.employees, e._super)
   
.where(e.annualSalary.gt(100000))
   
.fetch();

Frans

unread,
Sep 4, 2019, 12:57:03 PM9/4/19
to Querydsl
Works like a charm, thank you!
Reply all
Reply to author
Forward
0 new messages