[Logo]
Forums Register Login
How to get instance of child class, not parent class?
I have a child class which extends a parent class.

@Entity
public class ChildClass extends ParentClass implements Serializable

When I retrieve the child class (see below), I get an instance of the parent class even though the 1st parameter is the child class:
ChildClass childClass = em.find(ChildClass.class, childClassId);

1. I thought I am supposed to get an instance of the child class, not an instance of the parent class. Can someone explain why this is so?
2. How do I get an instance of the child class?

Thanks,
Lin

Hi Lin. Welcome to the Ranch!

Why do you think you aren't getting an instance of the child class? Is it throwing a ClassCastException, or is something else going on?
Thanks! Matthew. You're right it is throwing a ClassCastException.



Also, the check below produces the child class:


The child class is an association table with two fields: childID and parentID. The parent class has the parentID as the primary key and about 40 other fields. Modifications to these two entity classes are not allowed.

Lin

This thread has been viewed 598 times.

All times above are in ranch (not your local) time.
The current ranch time is
Nov 15, 2018 22:38:29.