JPA TABLE_PER_CLASS inheritance: as soon as select superclass entries?

I am using EclipseLink as a JPA provider. Next, I use the following inheritance structure TABLE_PER_CLASS

@javax.persistence.Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @NamedQueries({ @NamedQuery(name=Parent.QUERY_FIND_ALL, query="SELECT p FROM Parent p") }) public class Parent { // the class code follows here } @javax.persistence.Entity @NamedQueries({ @NamedQuery(name=Child.QUERY_FIND_ALL, query="SELECT c FROM Child c") }) public class Child extends Parent { // the class code follows here } 

Now the problem is that I want to get only the records of the parent class. But with the named query "SELECT p FROM Parent p", all records from the child table are also returned.

The selection or search code is as follows:

 public List findWithNamedQuery(String query) { return em.createNamedQuery(query).getResultList(); } 

Thus, the query is, for example, "SELECT p FROM Parent p".

How can I get only parent records and not all records of this inheritacne hierarchy?

In short: how can I leave all child records intact and return only parent records?

EDIT 1:
I use EclipseLink 2.0.1, but every time I try to use the axtavt solution with a type expression, I get the following error:

 "Invalid Type Expression on [my.domain.Parent]. The class does not have a descriptor, or a descriptor that does not use inheritance or uses a ClassExctractor for inheritance". 

I upgraded to the latest stable version 2.1.1 of EclipseLink, but this does not solve the problem.

+6
inheritance jpa table-per-class
source share
2 answers

A type expression combined with TABLE_PER_CLASS inheritance does not work in EclipseLink 2.1.1, this seems like an error.

See also JPA 2.0 answer : TYPE expression exception

+4
source share

If your version of EclipseLink supports JPA 2.0, you can write

 SELECT p FROM Parent p WHERE TYPE(p) = Parent 
+8
source share

All Articles