Hibernate – @AttributeOverride does not use inheritance

I tried to change the column name in the subclass table, but I will not change it with 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 created table The structure 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
)

< /div>

It helps to read the JavaDoc of @AttributeOverride:

May be applied to an entity that extends a mapped superclass or to an embedded field or property to override a basic mapping or id mapping defined by the mapped superclass or embeddable class (or embeddable class of one of its attributes).

When you When using InheritanceType.TABLE_PER_CLASS, you only need to switch to @MappedSuperclass of Employee. If you still need EM P table, you can inherit the second class from the super class.

I tried to change the column name in the subclass table, but it will not be changed 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() {
re turn salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}

but created The table structure is:

Employees:

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
)

It helps to read the JavaDoc of @AttributeOverride:

May be applied to an entity that extends a mapped superclass or to an embedded field or property to override a basic mapping or id mapping defined by the mapped superclass or embeddable class (or embeddable class of one of its attributes).

When you use InheritanceType.TABLE_PER_CLASS, you only need to switch to Employee @MappedSuperclass is fine. If you still need the EMP table, you can inherit the second class from this superclass.

Leave a Comment

Your email address will not be published.