Using KeyValueCollection with LookupField in 7.2

I have a KeyValueCollection to select a list of option items and its descriptions.

        <keyValueCollection id="prodLnDc">
            <loader id="prodLnDl">
                <query>
                    <![CDATA[select e.id.cdValue, e.descr from test1_Sy99refCodes e where e.id.cd = 'PRODLN']]>
                </query>
            </loader>
            <properties idProperty="prodLn">
                <property datatype="string" name="prodLn"/>
                <property datatype="string" name="descr"/>
            </properties>
        </keyValueCollection>

I used this in my lookupField:

<lookupField id="prodLnField" property="prodLn"
                             optionsContainer="prodLnDc"
                captionProperty="descr"/>

The screen shows the option descriptions and when I tried to update the property prodLn which is a String, it gives an error:

ClassCastException: com.haulmont.cuba.core.entity.KeyValueEntity cannot be cast to java.lang.String

How do I get the KeyValueCollection to return the option key (prodLn) string ?

Hi,
If the prodLn property is a string, the options of the lookup field should also be strings, but now they are of KeyValueEntity type. You can provide the options as a map of description->value strings programmatically (see also the docs):

<lookupField id="prodLnField" property="prodLn"/>
@Inject
private LookupField<String> prodLnField;

@Subscribe
public void onBeforeShow(BeforeShowEvent event) {
    Map<String, String> optionsMap = new LinkedHashMap<>();

    dataManager.loadValues(
                "select e.id.cdValue, e.descr from test1_Sy99refCodes e where e.id.cd = :cd")
            .parameter("cd", "PRODLN")
            .properties("prodLn", "descr")
            .list()
            .forEach(kve -> optionsMap.put(kve.getValue("descr"), kve.getValue("prodLn")));


    prodLnField.setOptionsMap(optionsMap);
}

OK Thanks. So I need to do some coding for this, rather than just configuring the XML.