Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
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

 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!