i have entities classes all contains id as primary key, can i create abstract class which contains all common fields and allow all classes extends this class as the following :
public abstract class CommonFields{
@Id
@Column(name = "ID")
private long id;
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
}
@Entity
@Table
public class B extends CommonFields{
String carModel;
}
@Entity
@Table
public class A extends CommonFields{
String name;
}
Thank You All
You can annotate the class with the common fields with @MappedSupperclass
@MappedSuperclass
public abstract class CommonFields{
@Id
@Column(name = "ID")
private long id;
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
}
From the @MappedSuperclass doc:
Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With