users@glassfish.java.net

[JPA] Join with inheritance

From: <glassfish_at_javadesktop.org>
Date: Thu, 23 Aug 2007 02:22:45 PDT

Hi,

Imagine I have the following entities:

[pre]
       Dept <---> Emp
                    / \
                   / \
                  v v
          EmpFullTime EmpPartTime
[/pre]

There's an [b]@Inheritance(strategy=InheritanceType.JOINED)[/b] involved: EmpFullTime and EmpPartTime extend Emp.

[b]EmpFullTime[/b] has a [b]salary[/b] property, while [b]EmpPartTime[/b] has an [b]hourlyRate[/b] property.

Now, the question:

Is it possible to construct a JPQL query, which will only fetch the department names and employee names for [b]full-time[/b] employees whose salary is greater than 1000?

In SQL, this could look like:

[pre]
    select d.name, e.name
    from dept d
           join emp e on (d.id = e.dept_id)
           join emp_full_time eft on (e.id = eft.id)
    where eft.salary > 1000[/pre]

In JPQL, however, if I try something like:

[pre]
    Query q = em.createQuery("select d from Dept d join d.emps e where e.salary > 1000");
[/pre]

I get an error:

[pre]
    ... unknown state or association field [salary] of class [test.jpa.entities.Emp]
[/pre]

While I understand why I get the error, I still wonder: Is it possible to create a JPQL query that can perform a join with only a part of an inheritance hierarchy? Is it possible to either specify which subclass I wish to join with, or to cast the base class to some subclass, so that the JPQL parser would know that I want a particular join with a particular subclass?

Best regards,
Bisser
[Message sent by forum member 'bisser' (bisser)]

http://forums.java.net/jive/thread.jspa?messageID=232259