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.annotation.WhenCreated
import io.ebean.annotation.WhenModified
import java.time.Instant
import javax.persistence.Id
import javax.persistence.MappedSuperclass
import javax.persistence.Version

@MappedSuperclass
open class BaseDomain {

  @Id
  var id: Long = 0

  @Version
  var version: Long = 0

  @WhenModified
  lateinit var whenModified: Instant

  @WhenCreated
  lateinit var whenCreated: Instant

}

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

Note that we are using Kotlin lateinit on the @WhenModified and @WhenCreated properties. We are treating these as non-null types that will have their values set when the bean is saved.

Customer extends BaseDomain

package org.example.domain

import javax.persistence.Entity

@Entity
class Customer(

  var name: String

) : BaseDomain()

Order extends BaseDomain

package org.example.domain

import java.time.LocalDate
import javax.persistence.*

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

  var orderDate: LocalDate,

  @ManyToOne
  var customer: Customer

): BaseDomain()

Edit Page