JPA specification states
Entities may extend non-entity classes as well as entity classes, and non-entity classes may extend entity classes.
@javax.persistence.MappedSuperclass annotation allows you to define this kind of mapping
@MappedSuperclass
public class MyThing implements Serializable {
private int myNumber;
private String myData;
// getter's and setter's
}
And
@Entity
@Table(name="MY_THING")
public class MyThingEntity extends MyThing {
}
As said by JPA specification
The MappedSuperclass annotation designates a class whose mapping information is applied to the entities that inherit from it.
And
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.
If you need to override some property defined by MyThing, use @AttibuteOverride (when you want to override a single property) or @AttibuteOverrides (when you want to override more than one property)
@Entity
@Table(name="MY_THING")
@AttributeOverride(name="myData", column=@Column(name="MY_DATA"))
public class MyThingEntity extends MyThing {
}
If you do not want to change your base class, you can use xml to define it as a @MappedSuperClass
Be aware: by default, the persistence provider will look 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 else. If you want to override a property, use @AttributeOverride as shown above