@AttributeOverride无法与继承一起使用 [英] @AttributeOverride not working with Inheritance

查看:50
本文介绍了@AttributeOverride无法与继承一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改子类表中的列名,但是并没有使用@AttributeOverride批注进行更改.

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:

员工:

CREATE TABLE EMP
(
  ID    NUMBER(10) NOT NULL,
  NAME  VARCHAR2(255 CHAR)
)

RegularEmployee:

RegularEmployee:

CREATE TABLE REGULAREMPLOYEE
(
  ID      NUMBER(10)                            NOT NULL,
  NAME    VARCHAR2(255 CHAR),
  SALARY  NUMBER(10)                            NOT NULL
)

推荐答案

它有助于读取 @AttributeOverride JavaDoc :

可以应用于扩展映射超类的实体,也可以应用于嵌入字段或属性,以覆盖由映射超类或可嵌入类(或其属性之一的可嵌入类)定义的基本映射或ID映射.

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

使用 InheritanceType.TABLE_PER_CLASS 时,您可以简单地为 Employee 切换到 @MappedSuperclass .如果仍然需要 EMP 表,则可以从该超类继承第二个类.

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.

这篇关于@AttributeOverride无法与继承一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆