Skip to main content
Version: Current

Data types - Views entities

View entities are classes generated by Genesis, which match your application's data model. The name of the view entity that is generated will be the name specified in its definition, but it is converted from snake case to camel case; for example, VIEW_NAME becomes ViewName. All table/view entities implement a common interface called DbEntity.

There are two types of view entity:

  • SingleCardinalityViewEntity
  • MultiCardinalityViewEntity

For more information, see our page about Views.

Index entities

You can also construct an index entity from a view entity. byPrimaryKey() will return an entity for the primary key. Additionally, for each index, there will be a by...() call with the index name.

Building a view entity

Generated ViewEntities are Kotlin data classes and can be built using the primary constructor. Just before the object is built, the object is validated to make sure all required fields have been set. In addition to DbEntity methods, there are some useful methods/properties, which are described below:

| Name | Signature | Description |
| --- | --- | --- |
| byPrimaryKey | `fun byPrimaryKey(): UniqueEntityIndex<*, *>` | gets entity by primaryKey |
| by{indexField} | `fun by{indexField}(): By{indexField}` | gets entity by index fields |
| toString | fun toString(): String | gets the string representation of the view with sensitive fields masked (for example, passwords) |
| toStringWithSensitivesUnmasked | `fun toStringWithSensitivesUnmasked(): String` | gets the string representation of view with sensitive fields(Ex: Password) unmasked |
| fieldToPropertyMap | `val fieldToPropertyMap: Map<String, KProperty1<V, Any?>>` | this is a class property that maps a field name to its property |

Examples

Given the following table and view definitions, declared as described here and here, the platform will generate view entities as shown in the examples.

// table definition    
table(name="CUSTOMER", id = 11002) {
CUSTOMER_NAME
CUSTOMER_ADDRESS
COUNTRY
CUSTOMER_PASSWORD sensitive "XXXXXX"
primaryKey {
CUSTOMER_NAME
}
}

// view definition
view("CUSTOMER_VIEW", CUSTOMER) {
fields {
CUSTOMER.CUSTOMER_NAME
CUSTOMER.CUSTOMER_PASSWORD
CUSTOMER.CUSTOMER_ADDRESS
CUSTOMER.COUNTRY
}
}

Examples:

val customerView = CustomerView("Customer_1", "PASSWORD", "London", "UK")  

customerView.toGenesisSet(listOf("CUSTOMER_NAME"))
// Output: CUSTOMER_NAME = Customer_1

customerView.toGenesisSetFormatted(listOf(ColumnConfig.Field.Aliased("COUNTRY", "CUSTOMER_COUNTRY")))
// Output: CUSTOMER_COUNTRY = UK

customerView.toString()
// Output: global.genesis.gen.view.entity.CustomerView{serialVersionUID='1', customerName=Customer_1, customerPassword=XXXXXX, customerAddress=London, country=UK, recordId={not-set}, timestamp={not-set}}

customerView.toStringWithSensitivesUnmasked()
// Output: global.genesis.gen.view.entity.CustomerView{serialVersionUID='1', customerName=Customer_1, customerPassword=PASSWORD, customerAddress=London, country=UK, recordId={not-set}, timestamp={not-set}}

customerView.get("CUSTOMER_NAME")
// Output: Customer_1