すべてのエンティティに抽象共通クラスを持たせようとしています。これが私のエンティティクラスと抽象クラスです:-
@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: An entity cannot be annotated with both @Entity and @MappedSuperclass: com.test.EmployeeEntity
エラーは明らかです @Entity と @MappedSuperclass の両方を一緒に持つことはできません。しかし、それは一般的な要件です。子クラスの親クラスのすべてのプロパティが必要です。私のコードでこれを達成するにはどうすればよいですか?