Akshay Sharma
Akshay Sharma

Reputation: 43

@AttributeOverride not working with Inheritance

I am trying to change the column name in the subclass table, but it is not getting changed with @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 getting 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
)

Upvotes: 4

Views: 3141

Answers (1)

Tobias Liefke
Tobias Liefke

Reputation: 9022

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).

As you use InheritanceType.TABLE_PER_CLASS you can simply switch to @MappedSuperclass for Employee. If you still need the EMP table you can inherit a second class from that superclass.

Upvotes: 4

Related Questions