@AttributeOverride doesn't work with inheritance
I am trying to change the name of a column in a subclass table, but it does not change using the @AttributeOverride annotation.
@Entity @Table(name="emp")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Employee {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
protected int id;
protected String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
@Table(name="RegularEmployee")
@AttributeOverrides({
@AttributeOverride(name="id", column=@Column(name="REGID")),
@AttributeOverride(name="name", column=@Column(name="REGNAME"))
})
public class RegularEmployee extends Employee {
private int salary;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
But the table structure being created is:
Employee:
CREATE TABLE EMP
(
ID NUMBER(10) NOT NULL,
NAME VARCHAR2(255 CHAR)
)
RegularEmployee:
CREATE TABLE REGULAREMPLOYEE
(
ID NUMBER(10) NOT NULL,
NAME VARCHAR2(255 CHAR),
SALARY NUMBER(10) NOT NULL
)
source to share
This helps to read the JavaDoc @AttributeOverride
:
Can be applied to an object that extends a mapped superclass, or to an inline field or property, to override the underlying mapping or identity mapping defined by the mapped superclass or nested class (or inline class of one of its attributes).
When you use InheritanceType.TABLE_PER_CLASS
, you can simply switch to @MappedSuperclass
for Employee
. If you still need a table EMP
, you can inherit a second class from this superclass.
source to share