'Hibernate JPA error when inherited form other class

I am trying to have a abstract common class for all my entity. Here is my Entity class and abstract class:-

@Entity
@MappedSuperclass
@Table(name="TBL_EMPLOYEES")
public class EmployeeEntity extends IdVersion {


    @Column(name="first_name")
    private String firstName;
    
    @Column(name="last_name")
    private String lastName;
    
    @Column(name="email", nullable=false, length=200)
    private String email;


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "EmployeeEntity [firstName=" + firstName +
                ", lastName=" + lastName + ", email=" + email   + "]";
    }
}


 @MappedSuperclass
 public abstract class IdVersion {

    @GeneratedValue
    @Column(name = "id")
    private Long id;

    @Version
    private Long version;

    @CreatedBy
    @Column(name = "created_by")
    private String createdBy;

    @CreatedDate
    private Instant created;

    @LastModifiedBy
    @Column(name = "updated_by")
    private String updatedBy;

    @LastModifiedDate
    private Instant updated;

}

Error:-

HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.howtodoinjava.demo.model.EmployeeEntity

What is wrong? How to fix it?

Updated question:

Now I have a sub class as follows:-

    @Entity
@Table(name="TBL_SUB_EMPLOYEES")
public class SubEmployeeEntity extends EmployeeEntity {

    @Column(name="sub_title")
    private String subTitle;

    @Column(name="sub_role")
    private String subRole;
}

I got error as this:-

Invocation of init method failed; nested exception is org.hibernate.AnnotationException: An entity cannot be annotated with both @Entity and @MappedSuperclass: com.test.EmployeeEntity

The error is clear I can't have both @Entity and @MappedSuperclass together. But it is common requirements. I need all the property from the parent class in my child class. How can I achieve this in my code?



Solution 1:[1]

You need to use @MappedSuperclass annotation in your abstract class.

ref: https://vladmihalcea.com/how-to-inherit-properties-from-a-base-class-entity-using-mappedsuperclass-with-jpa-and-hibernate/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Umesh Sanwal