Search code examples
inheritancejpaeclipselinksingle-table-inheritance

JPA Single Table Inheritance getType()


How do i retrieve the type parameter from parent class to determine which child class is being referenced. I have a simple example of Person table with discriminators Student, Teacher.. and i cannot find the getter for the type column? Is there in the DB but missing from the api. Any other way i can get the type of Person object i am referencing?


Solution

  • In an ideal world, you should not have to care about the type of the referenced person, and you should only access it polymorphically.

    If you're absolutely sure the referenced person is not a proxy, you can simply use the instanceof operator. Or you can modify the person entity to provide a getter for the discriminator column. Or you can override a getType() method in all subclasses in order for the objects to return their type.