The solution to code repetition (like for example wiring an id, version and/or timestamp fields in every Hibernate entity) is to inherit these properties from a superclass. This can be done using the @MappedSuperclass annotation. A mapped superclass designates a class whose mapping information is applied to the entities that inherit from it.


  • a mapped superclass has no separate table defined for it;
  • mapping information may be overridden in such subclasses by using the @AttributeOverride and @AssociationOverride annotations or corresponding XML elements.

Example: Concrete class as a mapped superclass

public abstract class AbstractEntity {

  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "ID", updatable = false, nullable = false)
  private Long id;

  @Column(name = "LAST_UPDATE_TIMESTAMP", nullable = false)
  private Date lastUpdateTimestamp;

  @Column(name = "VERSION", nullable = false)
  private int version = 0;

  public String toString() {
    return String.valueOf(id);

  protected void onPreUpdate() {
    lastUpdateTimestamp = new Date();

  public Long getId() {
    return id;

  public Date getLastUpdateTimestamp() {
    return lastUpdateTimestamp;

  public int getVersion() {
    return version;

@Table(name = "EMAIL")
public class Email extends AbstractEntity {

  @Column(name = "ADDRESS", nullable = false)
  private String address;

  public String getAddress() {
    return address;

  public void setAddress(String address) {
    this.address = address;

Categories & Tags