Model

io.ebean.Model is a base class that is extended by entity beans or MappedSuperclass beans to provide convenience methods for saving and deleting beans.

That is, the entity beans themselves then have methods such as save() and delete() for persisting.

Entity extending Model

@Entity
@Table(name="customer")
public class Customer extends Model {

  @Id
  long id;

  @Version
  long version;

  String name;
  ...
}

With Customer entity bean extending model we can save() or delete()it.

Customer customer = new Customer("Joe", "Montana");

// save using the default database
customer.save();

Default database

When we call the save() and delete() methods on Model it is obtaining the default database and using that to save or delete the entity bean.

 

Typical MappedSuperclass

Most often entity beans do not directly extend Model but instead we have a common MappedSuperclass that has commmon properties that our entity beans share.

@MappedSuperclass
public abstract class BaseDomain extends Model {

  @Id
  long id;

  @Version
  long version;

  @WhenCreated;
  Instant whenCreated;

  @WhenModified;
  Instant whenModified;

  ...
}

Our entity beans then extend BaseDomain inheriting the common properties.

@Entity
@Table(name="customer")
public class Customer extends BaseDomain {

  String firstName;

  String lastName;
  ...
}

Edit Page