Related: Trouble Shooting

Constructors

We don't need a default constructor and this means we can give our entity beans a constructor that takes arguments. Typically the arguments are properties that should always be supplied when creating a new bean of that type.

e.g. Customer must have a name

The name property is now in the constructor and also we can make it a non null type. Ebean knows it is a non null type and DDL defines the column as NOT NULL.

...
@Entity
class Customer(

  var name: String

) {

  @Id
  var id: Long = 0

}

The test code now changes to:

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

Kotlin non-null types

Ebean is aware of Kotlin non-null types and automatically maps those properties to database NOT NULL constraints.

e.g. Order must have a Customer and order date

The Order entity bean constructor takes both orderDate and customer and both are defined as NOT NULL from a code and DB perspective.

...

@Entity
@Table(name="orders")
class Order(

  var orderDate: LocalDate,

  @ManyToOne
  var customer: Customer
) {

  // other properties

}

Edit Page