This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic