MappedSuperclass
We use @MappedSuperclass
for common properties across many entity beans.
It is common to have a mapped superclass with @Id
, @Version
and @WhenModified
properties like the example below and common for the mapped superclass to extend Model.
package org.example.domain;
import io.ebean.Model;
import io.ebean.annotation.WhenCreated;
import io.ebean.annotation.WhenModified;
import jakarata.persistence.Id;
import jakarata.persistence.MappedSuperclass;
import jakarata.persistence.Version;
import java.time.Instant;
/**
* Base domain object with Id, version, whenCreated and whenModified.
*/
@MappedSuperclass
public abstract class BaseDomain extends Model {
@Id
long id;
@Version
Long version;
@WhenCreated
Instant whenCreated;
@WhenModified
Instant whenModified;
// getters and setters
...
}
package org.example.domain
import io.ebean.annotation.WhenCreated
import io.ebean.annotation.WhenModified
import java.time.Instant
import jakarata.persistence.Id
import jakarata.persistence.MappedSuperclass
import jakarata.persistence.Version
@MappedSuperclass
open class BaseDomain : Model() {
@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.
Customer extends BaseDomain
...
@Entity
public class Customer extends BaseDomain {
String name;
LocalDate registered;
...
package org.example.domain
import jakarata.persistence.Entity
@Entity
class Customer : BaseDomain() {
var name: String? = null
}
Kotlin has multiple constructor styles. See here for more information on the preferred constructor style for entity beans.