• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get instance of child class, not parent class?

 
Lin Ahmad
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Lin Ahmad
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!