3.5.3.1.4. KeyValue Containers

KeyValueContainer and KeyValueCollectionContainer are designed to work with KeyValueEntity. This entity can contain an arbitrary number of attributes which are defined at runtime.

The KeyValue containers define the following specific methods:

  • addProperty() - as the container can store entities with any number of attributes, you have to specify what attributes are expected by using this method. It accepts a name of the attribute and its type in the form of Datatype or a Java class. In the latter case, the class should be either an entity class or a class supported by one of the datatypes.

  • setIdName() is an optional method which allows you to define one of the attributes as an identifier attribute of the entity. It means that KeyValueEntity instances stored in this container will have identifiers obtained from the given attribute. Otherwise, KeyValueEntity instances get randomly generated UUIDs.

  • getEntityMetaClass() returns a dynamic implementation of the MetaClass interface that represents the current schema of KeyValueEntity instances. It is defined by previous calls to addProperty().

KeyValueContainer and KeyValueCollectionContainer can be defined in a screen XML descriptor using keyValueInstance and keyValueCollection elements.

XML definition of a KeyValue container must include the properties element that defines the KeyValueEntity attributes. The order of nested property elements should conform to the order of result set columns returned by the query. For example, in the following definition the customer attribute will get its value from o.customer column and the sum attribute from sum(o.amount) column:

<data readOnly="true">
        <keyValueCollection id="salesDc">
            <loader id="salesDl">
                <query>
                    <![CDATA[select o.customer, sum(o.amount) from sales_Order o group by o.customer]]>
                </query>
            </loader>
            <properties>
                <property name="customer" class="com.company.sales.entity.Customer"/>
                <property name="sum" datatype="decimal"/>
            </properties>
        </keyValueCollection>
</data>

KeyValue containers are designed only for reading data because KeyValueEntity is not persistent and cannot be saved by the standard persistence mechanisms.