Constructors

Ebean does not need a default constructor and this means we can give our entity bean a constructor that takes arguments. Typically the arguments are properties that should always be supplied when creating a new bean of that type (and by implication are typically non-nullable).

@Entity
public class Customer {

  @Id
  long id;

  @NotNull @Length(100)
  String name;

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

  // getters and setters

}
  
...
@Entity
class Customer(name : String) {

  @Id
  var id: Long = 0

  @Length(100)
  var name: String = name

}
  

Now when we create a new Customer we know we must create it with a name.

For Kotlin we can now make name a non-nullable type. Ebean will treat Kotlin non-nullable types as NOT NULL from a database perspective as well giving us a tighter model.

Customer customer = new Customer("Hello entity bean")
customer.save()
val customer = Customer("Hello entity bean")
customer.save()

Kotlin has multiple constructor styles. See here for more information on the preferred constructor style for entity beans.