EntityClass

abstract class EntityClass<ID : Any, out T : Entity<ID>>(val table: IdTable<ID>, entityType: Class<T>? = null, entityCtor: (EntityID<ID>) -> T? = null)

The entry point for the entities of one table: creating, finding, and deleting them.

Declare it as the companion object of the entity, normally through the variant matching the id type (IntEntityClass, LongEntityClass, UuidEntityClass, CompositeEntityClass, ...):

class Film(id: EntityID<Int>) : IntEntity(id) {
var title by Films.title

companion object : IntEntityClass<Film>(Films)
}

suspendTransaction {
val film = Film.newSuspend { title = "A New Hope" }
val titles = Film.all().toList().map { it.title }
Film.findById(film.id.value)?.delete()
}

Parameters

table

The table whose rows are mapped to entities managed by this class.

entityType

The Entity class to map. Defaults to the class that encloses this companion object.

entityCtor

Called to instantiate an entity for a row. Defaults to the entity's primary constructor, looked up by reflection on first access; pass a reference such as ::Film to skip that lookup.

Inheritors

Constructors

Link copied to clipboard
constructor(table: IdTable<ID>, entityType: Class<T>? = null, entityCtor: (EntityID<ID>) -> T? = null)

Properties

Link copied to clipboard
open val dependsOnColumns: List<Column<out Any?>>

The columns that this EntityClass depends on when maintaining relations with managed Entity instances.

Link copied to clipboard

The IdTable that this EntityClass depends on when maintaining relations with managed Entity instances.

Link copied to clipboard

Functions

Link copied to clipboard
open fun all(): SizedIterable<T>

Gets all the Entity instances associated with this EntityClass.

Link copied to clipboard
suspend fun attach(entity: Entity<ID>, force: Boolean = false)

R2DBC-specific helper. Registers an entity from a previous transaction in the current transaction's cache, allowing it to be read and modified in the new transaction.

Link copied to clipboard

Registers a reference as an immutable field of the parent entity class, which returns a child object of this EntityClass.

Link copied to clipboard
suspend fun count(op: Op<Boolean>? = null): Long

Counts the amount of Entity instances that conform to the op conditional expression.

Link copied to clipboard
fun find(op: () -> Op<Boolean>): SizedIterable<T>

Gets all the Entity instances that conform to the op conditional expression.

Link copied to clipboard
suspend fun findById(id: ID): T?

Gets an Entity by its raw id value.

open suspend fun findById(id: EntityID<ID>): T?

Gets an Entity by its EntityID value.

Link copied to clipboard
suspend fun findByIdAndUpdate(id: ID, block: (it: T) -> Unit): T?

Gets an Entity by its id value and updates the retrieved entity.

Link copied to clipboard
suspend fun findSingleByAndUpdate(op: Op<Boolean>, block: (it: T) -> Unit): T?

Gets a single Entity that conforms to the op conditional expression and updates the retrieved entity.

Link copied to clipboard

Returns a SizedIterable containing all entities with EntityID values from the provided ids list.

Link copied to clipboard

Returns a SizedIterable containing all entities with id values from the provided ids list.

Link copied to clipboard
suspend operator fun get(id: ID): T

Gets an Entity by its raw id value.

suspend operator fun get(id: EntityID<ID>): T

Gets an Entity by its EntityID value.

Link copied to clipboard
fun <ID2 : Any, T2 : Entity<ID2>> isAssignableTo(entityClass: EntityClass<ID2, T2>): Boolean

Returns whether the entityClass type is equivalent to or a superclass of this EntityClass instance's klass.

Link copied to clipboard

Returns an EntityFieldWithTransform delegate that caches the transformed value on first read of this same stored Unwrapped value.

Returns an EntityFieldWithTransform that extends transformation of an existing EntityFieldWithTransform and caches the transformed value on first read.

Link copied to clipboard
open fun new(init: T.() -> Unit): T

Creates a new Entity instance with the fields set in the init block, schedules its insert into the entity cache without flushing, and returns it — all without suspending.

open fun new(id: ID?, init: T.() -> Unit): T

Composite/explicit-id variant of new. Passing a non-null id is also the way to obtain an entity whose id is usable before the insert is issued.

Link copied to clipboard
open suspend fun newSuspend(init: suspend T.() -> Unit): T

Creates a new Entity instance with the fields set in the init block, schedules its insert, flushes the entity cache, and returns the fully-hydrated entity with its auto-generated id and database-generated columns populated.

open suspend fun newSuspend(id: ID?, init: suspend T.() -> Unit): T

Composite/explicit-id variant of newSuspend: creates a new Entity instance with the fields set in the init block and with the provided id, schedules its insert, flushes the entity cache, and returns the fully-hydrated entity.

Link copied to clipboard
@JvmName(name = "optionalBackReferencedOnNonNullable")
infix fun <TargetID : Any, Target : Entity<TargetID>, REF : Any> EntityClass<TargetID, Target>.optionalBackReferencedOn(column: Column<REF>): OptionalBackReference<TargetID, Target, ID, Entity<ID>, REF>

Registers an optional reference as an immutable field of the parent entity class, which returns a child object of this EntityClass or null if no child references the parent entity.

Link copied to clipboard

Registers an optional reference as a field of the child entity class, which returns a parent object of this EntityClass.

Composite-FK form of optionalReferencedOn. R2DBC counterpart of JDBC's optionalReferencedOn(IdTable<*>).

Link copied to clipboard

Registers an optional reference as an immutable field of the parent entity class, which returns a collection of child objects of this EntityClass that all reference the parent.

Link copied to clipboard
infix fun <REF : Any> referencedOn(column: Column<REF>): Reference<ID, T, REF>

Registers a reference as a field of the child entity class, which returns a parent object of this EntityClass.

infix fun referencedOn(table: IdTable<*>): Reference<ID, T, Any>

Composite-FK form of referencedOn. R2DBC counterpart of JDBC's referencedOn(IdTable<*>).

Link copied to clipboard

Registers a reference as an immutable field of the parent entity class, which returns a collection of child objects of this EntityClass that all reference the parent.

Link copied to clipboard
suspend fun reload(entity: Entity<ID>, flush: Boolean = false): T?

Reloads the fields of an entity from the database and returns the entity as a new object.

Link copied to clipboard
fun removeFromCache(entity: Entity<ID>)

Removes the specified entity from the current EntityCache, as well as any stored references to or from the removed entity.

Link copied to clipboard
open fun searchQuery(op: Op<Boolean>): Query

Returns a Query to select all columns in dependsOnTables with a WHERE clause that includes the provided op conditional expression.

Link copied to clipboard
fun testCache(id: EntityID<ID>): T?

Searches the current EntityCache for an Entity by its EntityID value.

Link copied to clipboard
suspend fun <SID : Any> warmUpLinkedReferences(references: List<EntityID<SID>>, linkTable: Table, forUpdate: Boolean? = null, optimizedLoad: Boolean = false): List<T>

Returns a list of retrieved Entity instances whose reference column matches any of the EntityID values in references. Both the entity's source and target reference columns should have been defined in linkTable.

Link copied to clipboard
suspend fun <SID> warmUpOptReferences(references: List<SID>, refColumn: Column<SID?>, orderBy: Array<Pair<Expression<*>, SortOrder>>? = null): List<T>

Returns a list of retrieved Entity instances whose refColumn optionally matches any of the id values in references.

Link copied to clipboard
suspend fun <SID> warmUpReferences(references: List<SID>, refColumn: Column<SID>, orderBy: Array<Pair<Expression<*>, SortOrder>>? = null): List<T>

Returns a list of retrieved Entity instances whose refColumn matches any of the id values in references.

Link copied to clipboard
fun wrap(id: EntityID<ID>, row: ResultRow?): T

Returns an Entity with the provided EntityID value, or, if an entity was not found in the current EntityCache, creates a new instance using the data in row.

Link copied to clipboard
fun wrapRow(row: ResultRow): T
fun wrapRow(row: ResultRow, alias: Alias<IdTable<*>>): T
fun wrapRow(row: ResultRow, alias: QueryAlias): T

Wraps the specified ResultRow data into an Entity instance.

Link copied to clipboard

Returns a SizedIterable containing entities generated using data retrieved from a database result set in rows.