I'm trying to create a query which should return the subtypes (List of InternalTask and ExternalTask). This works great, but I want to add a where clause in the query for one of the subtypes. I have tried the following:
Entities:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TASK_TYPE")
public abstract class Task {
    ...
}
@Entity
@DiscriminatorValue("INTERNAL")
public class InternalTask extends Task {
    ...
    private Employee employee;
    ...
}
@Entity
@DiscriminatorValue("EXTERNAL")
public class ExternalTask extends Task {
    ...
}
Function:
public List<? extends Task> findTasks(TaskSearch taskSearch) {
    JPAQuery query = new JPAQuery(entityManager);
    QTask task = QTask.task;
    BooleanBuilder where = new BooleanBuilder();
    if (taskSearch.getEmployee() != null) {
        where.and(task.instanceOf(InternalTask.class).and(task.as(QInternalTask.class).employee.eq(taskSearch.getEmployee())));
    }
    query.from(task).where(where).orderBy(task.deadline.asc());
    return query.list(task);
}
Error:
An error occurred while parsing the query filter "select task_
from Task task_
where (type(task_) = ?1 and task_.employee = ?2)
order by task_.deadline asc". Error message: No field named "employee" in "Task". Did you mean "deadline"? Expected one of the available field names in "com.exampe.Task": "[deadline]".
As you can see it is translated to a select on the Task entity which doesn't know the subtype InternalTask. Is there a way to accomplish a where clause on a subtype?
I found a solution by adding a subQuery:
public List<? extends Task> findTasks(TaskSearch taskSearch) {
    JPAQuery query = new JPAQuery(entityManager);
    QTask task = QTask.task;
    QInternalTask internalTask = QInternalTask.internaltask;
    BooleanBuilder where = new BooleanBuilder();
    if (taskSearch.getEmployee() != null) {
        JPASubQuery from = new JPASubQuery().from(internalTask)
                .where(internalTask.employee.eq(taskSearch.getEmployee()).and(internalTask.id.eq(task.id)));
        where.and(from.exists());
    }
    query.from(task).where(where).orderBy(task.deadline.asc());
    return query.list(task);
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With