Related: Trouble Shooting

MappedSuperclass

We can use @MappedSuperclass for common properties used across many entity beans.

It is common to have a mapped superclass with @Id, @Version and @WhenModified properties like:

package org.example.domain;

import io.ebean.Model;
import io.ebean.annotation.WhenCreated;
import io.ebean.annotation.WhenModified;

import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import java.time.Instant;

/**
 * Base domain object with Id, version, whenCreated and whenModified.
 */
@MappedSuperclass
public abstract class BaseDomain {

  @Id
  long id;

  @Version
  Long version;

  @WhenCreated
  Instant whenCreated;

  @WhenModified
  Instant whenModified;

  // getters and setters
  ...

}

Our entity beans then extend this to inherit the common properties.

Customer extends BaseDomain

...
@Entity
public class Customer extends BaseDomain {

  String name;

  LocalDate registered;
  ...

Order extends BaseDomain

...
@Entity
public class Order extends BaseDomain {

  @ManyToOne
  Customer customer;

  LocalDate orderDate;
  ...

Edit Page