JPA Specification Specification
Entities can extend non-entity classes as well as entity classes , and classes without entities can extend entity classes.
@ javax.persistence.MappedSuperclass annotations allows you to define this kind of display
@MappedSuperclass public class MyThing implements Serializable { private int myNumber; private String myData;
and
@Entity @Table(name="MY_THING") public class MyThingEntity extends MyThing { }
As stated in the JPA specification
The designation MappedSuperclass denotes a class whose mapping information applies to objects that inherit from it .
and
The class indicated by the MappedSuperclass annotation can be mapped in the same way as an object, except that mappings will only apply to its subclasses , since there is no table for the mapped superclass itself.
If you need to override a property defined by MyThing, use @AttributeOverride (if you want to override one property) or @AttributeOverrides (if you want to override several properties)
@Entity @Table(name="MY_THING") @AttributeOverride(name="myData", column=@Column(name="MY_DATA")) public class MyThingEntity extends MyThing { }
and
@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 { }
If you do not want to change the base class, you can use xml to define it as @MappedSuperClass
Be aware: by default, the save provider will search in the META-INF directory for a file named orm.xml
<?xml version="1.0" encoding="UTF-8"?> <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0"> <mapped-superclass class="MyThing"> </mapped-superclass> </entity-mappings>
Nothing more . If you want to override a property, use @AttributeOverride, as shown above.