JPA规范说明
Entities may extend non-entity classes as well as entity classes, and non-entity classes may extend entity classes.
@ javax.persistence.MappedSuperclass注释允许您定义此类映射
@MappedSuperclass
public class MyThing implements Serializable {
private int myNumber;
private String myData;
// getter's and setter's
}
和
@Entity
@Table(name="MY_THING")
public class MyThingEntity extends MyThing {
}
如JPA规范所述
The MappedSuperclass annotation designates a class whose mapping information is applied to the entities that inherit from it.
和
A class designated with the MappedSuperclass annotation can be mapped in the same way as an entity except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself.
如果你需要覆盖由MyThing定义的一些属性,使用@AttributeOverride(当你想覆盖单个属性)或@AttributeOverrides(当你想覆盖多个属性)
@Entity
@Table(name="MY_THING")
@AttributeOverride(name="myData", column=@Column(name="MY_DATA"))
public class MyThingEntity extends MyThing {
}
和
@Entity
@Table(name="MY_OTHER_THING")
@AttributeOverrides({
@AttributeOverride(name="myData1", column=@Column(name="MY_DATA_1")),
@AttributeOverride(name="myData2", column=@Column(name="MY_DATA_2"))
})
public class MyOtherThingEntity extends MyThing {
}
如果不想更改基类,可以使用xml将其定义为@MappedSuperClass
注意:默认情况下,持久性提供程序将在META-INF目录中查找名为orm.xml的文件
没有其他的。如果要覆盖属性,请使用@AttributeOverride,如上所示