我正在尝试为我的所有实体创建一个抽象的公共类。这是我的实体类和抽象类:-
@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;
}
错误:-
HibernateJpaConfiguration.class]: init 方法调用失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:com.howtodoinjava.demo.model.EmployeeEntity
怎么了?如何解决?
更新的问题:
现在我有一个子类如下: -
@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;
}
我得到了这样的错误: -
调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:不能同时使用 @Entity 和 @MappedSuperclass 注释实体:com.test.EmployeeEntity
错误很明显我不能同时拥有@Entity 和@MappedSuperclass。但这是共同的要求。我需要子类中父类的所有属性。如何在我的代码中实现这一点?