Database Entities
When defining entities, make sure to add parameters that cannot be computed by jakarta to the constructor (uid in this example), instead of making them nullable. Other variables, such as id or createdAt may be added to the class body.
kotlin
@Entity
class Example(val uid: String) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null
companion object {
// Prefix for UIDs, must be unique to this entity.
const val UID_PREFIX = "ex"
// function to generate UIDs for this entity.
// used by calling `Example.generateUid()`
fun generateUid() = StringGenerator.generateUid(UID_PREFIX)
}
}toView
For every entity, there should be at least one implementation of a toView method that returns a (data) class which only contains values that are 'okay' for a client to access. Usually the id field is excluded in favour of a uid.
kotlin
// use @Schema to set the correct name in the open-api schema.
@Schema(name = "Example")
data class ExampleView(
val uid: String,
)
fun Example.toView(): ExampleView = ExampleView(
uid = this.uid
)EntityConverterService
To do the inverse of toView, the EntityConverterService should be used.
kotlin
class EntityConverterService {
// adding parameters to this function is allowed
fun buildExample(): Example = Example(
uid = this.uid
)
}