Subclass Ordering in JPA InheritanceType.JOINED Tables

I am using JPA with EclipseLink and InheritanceType.JOINED

Structure:

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name="PARENT")
@DiscriminatorColumn(name="TYPE")
public abstract class Parent { ... }

@Entity
@Table(name="CHILD_A")
@DiscriminatorValue("A")
public class ChildA extends Parent { ... }

@Entity
@Table(name="CHILD_B")
@DiscriminatorValue("B")
public class ChildB extends Parent { ... }

      

If I use the Criteria-API to query data of type Parent.class

, the resulting SQL file looks like

SELECT t0.ID, ..., t1.ID, ..., t2.ID FROM 
PARENT t0 
LEFT OUTER JOIN CHILD_A t1 ON (t1.ID = t0.ID) 
LEFT OUTER JOIN CHILD_B t2 ON (t2.ID = t0.ID) 
WHERE ... 
ORDER BY t0.PROPERTY ASC

      

This might be a silly question, but is it possible to use a combined property (from CHILD_A or CHILD_B) in the ORDER clause via the Criteria-API?

I did the following:

CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder();
CriteriaQuery<Parent> q = cb.createQuery(Parent.class);
Root<Parent> c = q.from(Parent.class);

// FIXME: Don't know how to add orderBy of subclass property

TypedQuery<Parent> query = em.createQuery(q);
return query.getResultList();

      

Can anyone tell me if this is the right approach to place an order and how is it done?

+3


source to share





All Articles