Data types - Table entities
Table entities are classes generated by Genesis that match your applications's data model. The generated entity name is based on the table name, but will be camel case.
For example, TABLE_NAME
becomes TableName
.
All table/view entities implement a common interface called DbEntity.
Index entities
Tables, like views, have index entities. There are also convenient methods that construct an index entity from the table entity. byPrimaryKey()
will return an entity for the primary key. Additionally, for each index, there will be a by...()
call with the index name.
Builder
All table entities come with builders to help construct these objects. In Kotlin, the builder works as a lambda in which the field values are set, and the object is built after the lambda call is completed. In Java, the builder is a fluent interface, where fields are set and the object is built in a final build
call.
Just before the object is built, the object is validated to make sure all required fields have been set.
- Kotlin
- Java
val trade = Trade {
tradeId = id
tradeType = type
tradeDate = DateTime.now()
currencyId = "USD"
quantity = 500
price = 2.0
}
Trade trade = Trade.builder()
.setTradeType(tradeType)
.setTradeId(tradeId)
.setTradeDate(now())
.setCurrencyId("USD")
.setQuantity(50)
.setPrice(2.0)
.build();
Auditable tables
When a table is audited, the table entity can be easily converted to its audited counterpart by calling the toAuditEntity
function.
- Kotlin
- Java
val tradeAudit = trade.toAuditEntity(
auditEventType = "trade modify"
auditEventDatetime = DateTime.now(),
auditEventText = "trade was modified in event",
auditEventUser = user,
)
TradeAudit = trade.toAuditEntity(
"trade modify"
DateTime.now(),
"trade was modified in event",
user
);
Some of the most useful methods
Get an entity by primary key
fun byPrimaryKey(): UniqueEntityIndex<*, *>`
This function gets an entity by primaryKey.
Get an entity by index fields
fun by{indexField}(): By{indexField}`
This function gets an entity by index fields.
Get a string representation of a view
fun toString(): String'
This function gets the string representation of the view, but any sensitive fields (such as passwords) are masked.
Get a string representation including sensitive fields
fun toStringWithSensitivesUnmasked(): String`
This function gets the string representation of the view. Sensitive fields (such as passwords) are unmasked.
Set a table field to a value
operator fun <T> set(field: TableField<*, T>, value: T?)
This sets a table field to the value provided.
Examples
Given the following table definition, declared as described here, the platform will generate table entities as shown in the examples.
table(name="CUSTOMER", id = 11002) {
CUSTOMER_NAME
CUSTOMER_ADDRESS
COUNTRY
CUSTOMER_PASSWORD sensitive "XXXXXX"
primaryKey {
CUSTOMER_NAME
}
}
Examples:
val customer = Customer {
customerName = "Customer_1"
customerPassword = "PASSWORD"
customerAddress = "London"
country = "UK"
}
customer.toGenesisSet(listOf("CUSTOMER_NAME"))
// Output: CUSTOMER_NAME = Customer_1
customer.toGenesisSetFormatted(listOf(ColumnConfig.Field.Aliased("COUNTRY", "CUSTOMER_COUNTRY")))
// Output: CUSTOMER_COUNTRY = UK
customer.toString()
// Output: Customer{serialVersionUID='1', customerName=Customer_1, customerAddress=London, country=UK, customerPassword=XXXXXX, recordId={not-set}, timestamp={not-set}}
customer.toStringWithSensitivesUnmasked()
// Output: Customer{serialVersionUID='1', customerName=Customer_1, customerAddress=London, country=UK, customerPassword=PASSWORD, recordId={not-set}, timestamp={not-set}}
customer.get(CUSTOMER.CUSTOMER_ADDRESS)
// Output: London
customer.set(CUSTOMER.CUSTOMER_ADDRESS, "Manchester")