Skip to main content
Version: Previous

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 and properties, which are described below:

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.

Map a field name to its property

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