Problem loading derived class with EntityManager.getReference(BaseClass, value)

I defined a class B that is derived from class A (single table inheritance in database). In class B an additional property is defined.

Loading a class B with EntityManager.find(A.class, value) returns a instance of type B without problem.

But when using EntityManager.getReference(A.class, value) an error occures when an instance of type B should be loaded: “IllegalArgumentException: object is not an instance of declaring class”. The implementation tries to assign the additional property of B to an instance of A, which is not possible.

Hi Stefan,

I cannot reproduce the problem. This code works:


try (Transaction tx = persistence.createTransaction()) {
    EntityManager em = persistence.getEntityManager();
    A entity = em.getReference(A.class, idOfB);
    System.out.println(entity);
    tx.commit();
}

providing that B extends A and idOfB is an identifier of a B instance in the database. It returns an instance of A though, because it doesn’t go to the database and check what type it actually is.

Please provide more information on your situation.