Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 341
zbryan Avatar asked Jun 09 '16 06:06

zbryan


1 Answers

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.

like image 153
Neil Stockton Avatar answered Oct 14 '22 14:10

Neil Stockton