Reputation: 763
I have a inheritance structure with to classes, let's say Parent (as the root class) and Child as the subclass.
So with JPA 2.0 no I can select only the Parent class by using
SELECT p FROM Parent p WHERE TYPE(p) = Parent
This only should return entries of Parent and not also the entries of child.
But with my EclipseLink 2.1.1 and MySql on Glassfish v3, I always 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".
Additionally, I define no orm-mapping by hand. This is all done automatically on deployment, I think.
Is there something I have to add to my Parent /Child class (an annotation i.e.) to declare the inheritance structure? (But I think this shouldn't be necessary, because the inheritance is declared by Java, is it?)
EDIT:
One important aspect I've didn't mentioned is that I'm using the inheritance method "TABLE_PER_CLASS".
Upvotes: 2
Views: 4459
Reputation: 15239
Forget what I said before. This will work for SINGLE_TABLE
strategy:
@Entity
@Table(name="PERSON")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="GENDER", discriminatorType=DiscriminatorType.STRING, length=6)
public abstract class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="PERSON_PERSONID_GENERATOR", sequenceName="PERSON_ID_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PERSON_PERSONID_GENERATOR")
@Column(name="PERSON_ID", updatable=false, unique=true, nullable=false, precision=22)
private long personId;
@Column(nullable=false, length=32)
private String surname;
@Column(name="GIVEN_NAME", nullable=false, length=32)
private String givenName;
// ...
}
@Entity
@DiscriminatorValue("FEMALE")
public class Daughter extends Person implements Serializable {
@Column(name="NUMBER_OF_DOLLS", precision=22)
private int numberOfDolls;
// ...
}
@Entity
@DiscriminatorValue("MALE")
public class Son extends Person implements Serializable {
@Column(name="NUMBER_OF_TOY_CARS", precision=22)
private Integer numberOfToyCars;
// ...
}
// JUnit test method
public void testInheritance() {
EntityManager em = createNewEntityManagerInstance();
EntityTransaction tx = em.getTransaction();
tx.begin();
Daughter d = new Daughter();
d.setGivenName("Sue");
d.setSurname("Smith");
d.setNumberOfDolls(5);
em.persist(d);
Son s = new Son();
s.setGivenName("Joe");
s.setSurname("Smith");
s.setNumberOfToyCars(8);
em.persist(s);
tx.commit();
Query q;
List<?> personList;
Person p;
q = em.createQuery("SELECT p FROM Person p WHERE TYPE(p) = Daughter");
personList = q.getResultList();
assertEquals(1, personList.size());
p = (Person)personList.get(0);
System.out.println(
"This Daughter is: " + p.getGivenName() + " " + p.getSurname());
q = em.createQuery("SELECT p FROM Person p WHERE TYPE(p) = Son");
personList = q.getResultList();
assertEquals(1, personList.size());
p = (Person)personList.get(0);
System.out.println(
"This Son is: " + p.getGivenName() + " " + p.getSurname());
q = em.createQuery("SELECT p FROM Person p");
personList = q.getResultList();
assertEquals(2, personList.size());
for (Object o : personList) {
assertTrue(o instanceof Person);
p = (Person)o;
System.out.println(
"This person is: " + p.getGivenName() + " " + p.getSurname());
}
em.close();
}
The database (I'm using Oracle) DDL looks like this:
CREATE TABLE "DEV"."PERSON"
(
"PERSON_ID" NUMBER NOT NULL ENABLE,
"GIVEN_NAME" VARCHAR2(32 BYTE) NOT NULL ENABLE,
"SURNAME" VARCHAR2(32 BYTE) NOT NULL ENABLE,
"GENDER" VARCHAR2(6 BYTE) NOT NULL ENABLE,
"NUMBER_OF_DOLLS" NUMBER,
"NUMBER_OF_TOY_CARS" NUMBER,
CONSTRAINT "PERSON_PK" PRIMARY KEY ("PERSON_ID")
);
Now you said that you're trying to use TABLE_PER_CLASS
strategy. I can't help you there, since the JPA 2.0 spec says that vendors are not required to support it. Your implementation may not support it properly via the JPA interfaces.
Upvotes: 4
Reputation: 763
the solution by Jum Tough doesn't work for me. But the problem is only available in combination with the inheritance method "TABLE_PER_CLASS" with all other inheritance strategies the type expression works just fine.
I think this is a bug in EclipseLink, because I can't find nothing in the JPA 2 specs which explicity excludes says that the type expression doesn't work with TABLE_PER_CLASS.
Gerry
Upvotes: 0