Default constructor

Ebean enhancement will automatically add a default constructor if one does not already exist on the entity bean.

Example - Customer, No default constructor

In the example Customer entity bean below there is no default constructor and Ebean's enhancement will add one automatically.

/**
 * Customer entity bean with no default constructor.
 */
@Entity
public class Customer {

  public static final CustomerFinder find = new CustomerFinder();

  @NotNull @Size(max = 100)
  String name;

  ...

  // enhancement will add a default constructor
  // if there is not one on the entity bean

  public Customer(String name) {
    this.name = name;
  }

  ... // getters, setters etc

Partial objects

Note that you can make fields final if you wish, there is no restriction to Ebean for partially loading the customer bean (without the name property) or creating reference beans (that only have their @Id property loaded).

// fetch partially populated beans that
// don't have the name property loaded

List<Customer> customers =
  Customer.find.where()
    .name.startsWith("Rob")
    .select("id") // only select id property
    .findList();

Reference bean

Ebean can construct reference beans which only have their @Id property loaded.

// reference bean with only @Id property loaded
Customer refBean = Customer.find.ref(42);

// reference beans don't hit the database unless
// lazy loading is invoked

// invoke lazy loading as name property is not loaded
String name = refBean.getName();

Example - Country, No setters

With the Country entity bean below there are only 2 properties and these are both set in the constructor. In this case there are no setters on this entity bean.

/**
 * Country entity bean with no default constructor and
 * no setters (only getters).
 */
 @Entity
 public class Country extends Model {

   public static final CountryFinder find = new CountryFinder();

   @Id @Size(max = 2)
   final String code;

   @NotNull @Size(max = 60)
   final String name;

   // enhancement will add a default constructor

   public Country(String code, String name) {
     this.code = code;
     this.name = name;
   }

   // getters only for Country bean

   public String getCode() { return code; }

   public String getName() { return name; }

 }

Ebean can still support partially loaded entity beans and reference beans.

// insert a country
new Country("NZ", "New Zealand").save();

// reference bean (only has @Id property loaded)
Country nzRefBean = database.getReference(Country.class, "NZ");

// reference bean using a "finder"
Country nzRefBean = Country.find.ref("NZ");

Kotlin

Country written in Kotlin

The code and name properties are non-null / required (no ? after the type declaration).

@Entity
@Table(name = "country")
class Country (

    @Id @Size(max = 2)
    var code: String, // non-null type

    @NotNull @Size(max = 60)
    var name: String  // non-null type

) : Model() {

  override fun toString(): String {
    return "code:$code name:$name";
  }

  companion object : CountryFinder() {}
}

... using the Customer bean in Kotlin.

Country("NZ", "New Zealand").save()

// reference bean
val nzRef = Country.ref("NZ")

// finder & query bean use
val nz = Country.where()
    .code.equalTo("NZ")
    .findOne()

Customer written in Kotlin

The customer name property is declared as always non-null (no ? after the type declaration for name) and must be passed when constructing a new customer instance.

@Entity
@Table(name = "customer")
class Customer(

  @NotNull @Size(max = 100)
  var name: String,  // non-null type

  @SoftDelete
  var deleted: Boolean = false,

  var registered: Date? = null,

  @Size(max = 1000)
  var comments: String? = null,

  @ManyToOne(cascade = arrayOf(CascadeType.ALL))
  var billingAddress: Address? = null,

  @ManyToOne(cascade = arrayOf(CascadeType.ALL))
  var shippingAddress: Address? = null,

  @OneToMany(mappedBy = "customer", cascade = arrayOf(CascadeType.PERSIST))
  var contacts: MutableList<Contact> = ArrayList()

) : BaseModel() {

  companion object find : CustomerFinder() {}

  override fun toString(): String {
    return "customer(id:$id name:$name)";
  }
}

... using Customer in Kotlin.

// new customer (requires name)
val rob = Customer("Rob")
rob.save()

// reference bean
val refBean = Customer.ref(42L)

// finder / query bean use
val customers =
  Customer.find.where()
    .name.istartsWith("rob")
    .findList()