6

I have these entities;

Person.class

@Entity
@Table(name = "person")
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Person implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "person_id")
private long personID;

StudentBean.class

@Entity
@Table(name = "student_information")
public class StudentBean extends Person {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long studentID;

My goal is to save a student record in 1 table together with the details in the Person class and StudentBean class, but I have encountered these exception

org.hibernate.mapping.JoinedSubclass cannot be cast to org.hibernate.mapping.RootClass

on my research I stumbled upon this thread [Spring 3.1 Hibernate 4 exception for Inheritance cannot be cast to org.hibernate.mapping.RootClass

Based from it, the reason for the exception is due to the @Id in the Parent and Child class. But if I removed the @Id in either of my class, it would not allow me to save the object since I don't have an identifier with my entity.

I want a Person ID to identify a person outside the school and a Student ID to identify a person inside the school, is this possible? if not, how can I remove the PK of the Person class but still I can extend it and save it into another table.

lastly, what is the best way to implement this design, Person can be extended for Student Class, Teacher Class, SchoolPrincipal Class

Community
  • 1
  • 1
zbryan
  • 805
  • 2
  • 12
  • 24
  • problem solved by using @MappedSuperclass in the Person class – zbryan Jun 09 '16 at 07:38
  • MappedSuperclass is when you don't want the Parent to be a table in your DB. There is some strategies on Hibernate to deal with Inheritance. See https://www.baeldung.com/hibernate-inheritance – Guilherme Alencar May 31 '21 at 18:39

2 Answers2

23

Your mapping is wrong. You cannot have @Id in a superclass and then add another @Id in a subclass. The "id" should uniquely identify any object in an inheritance tree (and "Student" is also a "Person").

If you want to have different types of "person" you could have an extra field in Person about whether they are in education. But don't see how that should be part of the "id"

The error message is stupid and tells you nothing about the real cause; take that part up with Hibernate developers.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
  • Let's say I would drop the Person class, but its field(first name, last name) would be placed in the Student class and Teacher class. How can I do it without explicitly declaring the first name and last name on both classes? – zbryan Jun 09 '16 at 07:16
  • I've really no idea why you would contemplate dropping Person (and duplicating info). Besides which a question on Stackoverflow is supposed to be specific ... and yours seemed to be about why you get that error. There is no "right way" to design classes ... that is all "opinion based" and Stackoverflow is not the place for that. – Neil Stockton Jun 09 '16 at 07:40
  • indeed, having id on the children classes is rendered useless. – thechaoticpanda Sep 24 '19 at 09:23
  • It's the Hibernate concept that's wrong. A subclass should have its own additional @Id – Alessandro C Apr 11 '20 at 16:48
0

Since the inheritance type is joined, it means you have a strategy that will create a table for each of the classes referencing one another through the use of foreign keys.

The superclass must therefore be a @MappedSuperClass and not an Entity

DrFlash
  • 21
  • 3