JPA 2.0 CriteriaQuery with a predicate of type that is a class or any subclass

Assuming the following classes and entity hierarchy:

@Entity
Class Ticket {

    @ManyToOne(optional = true)
    private Customer customer;    

}

@Entity
Class Customer {}

@Entity
Class Person extends Customer {}

@Class CustomPerson extends Person {}

How can I request for all tickets that have customertype Person or any subclass of Person (i.e. CustomPerson)?

I can easily create a predicate by type:

Predicate p = criteriaBuilder.equal(Ticket_...type(), criteriaBuilder.literal(Person.class));

but it will filter out CustomPerson.

If there is no general way to handle this (in JPA 2.0), I could overcome this problem if I could define all entities extending Person at runtime; in this case I could use

Predicate p = criteriaBuilder.in(Ticket_...type(), listOfPersonSubclasses);
+4
source share
2 answers

JPA 2.0 , , Person. "Select t from Person p, ticket t where t.customer=p"

JPA 2.1 "", , . , , . https://wiki.eclipse.org/EclipseLink/Release/2.5/JPA21#Treat

where, :

"Select t from Ticket t join treat(t.customer as Person) p"
+3

JPA , ( ).

:

org.eclipse.persistence.sessions.Session session = 
     ((org.eclipse.persistence.internal.jpa.EntityManagerImpl) 
                 em.getDelegate()).getSession();
0
source

All Articles