Aks 1316
Aks 1316

Reputation: 382

Hibernate inheritance error java.lang.ClassCastException: org.hibernate.mapping.UnionSubclass cannot be cast to org.hibernate.mapping.RootClass

I am trying to inherit Auditable class to my user class so I can keep track of each user created and updated. But while adding @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) its showing me error.

java.lang.ClassCastException: org.hibernate.mapping.UnionSubclass cannot be cast to org.hibernate.mapping.RootClass

I have tried different solutions for this but I am not able to understand actual reason for this error.

Auditable.java

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Auditable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    protected String createdBy;
    protected Date createdOn;
    protected String updatedBy;
    protected Date updatedOn;
}

User.java

@Entity
public class User extends Auditable{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String userId;
    private String userName;
    private String password;
    private String userType;
    private String firstName;
    private String lastName;
    private String gender;
    private Date dob;
    private String email;
}

Now while creating new User its not allowing me to save user its showing ClassCaseException. So I am not able to understand how to inherit auditable class.

Upvotes: 0

Views: 2718

Answers (1)

Mehran Hassani
Mehran Hassani

Reputation: 119

You cannot have two 'id' fields since you are making one table for the object and its superclass.

Upvotes: 1

Related Questions