get distinct values in lookupfield

Hello,
I am trying to get the distinct values in the lookup field from custom query but somehow the “distinct” keyword doesn’t help.
I also enabled the application property (inMemoryDistinct) as mentioned in the documents .
I can still see the repeated values in the lookup field.

browse.xml file:


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        caption="msg://browseCaption"
        class="com.company.zeusv2.web.annotation.AnnotationBrowse"
        focusComponent="AnnotationFieldDataGrid"
        lookupComponent="AnnotationFieldDataGrid"
        messagesPack="com.company.zeusv2.web.annotation">
    <dsContext>
        <collectionDatasource id="annotationsDs"
                              class="com.company.zeusv2.entity.Annotation"
                              view="_local">
            <query>
                <![CDATA[select distinct e from zeusv2$Annotation e]]>
                <filter>
                    <and>
                        <c>e.type = :custom$typeSearch</c>
                    </and>
                </filter>
            </query>
        </collectionDatasource>
    </dsContext>
    <dialogMode height="600"
                width="800"/>
    <layout expand="AnnotationFieldDataGrid"
            spacing="true">
        <label value="Annotation Table"/>
        <lookupField id="typeSearch"/>
        <dataGrid id="AnnotationFieldDataGrid"
                  datasource="annotationsDs"
                  editorEnabled="true"
                  width="100%">
            <columns>
                .
               .
            </columns>
            <rowsCount/>
        </dataGrid>
    </layout>
</window>

Controller.java :


package com.company.zeusv2.web.annotation;

import com.haulmont.cuba.gui.components.AbstractLookup;
import com.haulmont.cuba.gui.components.DataGrid;
import com.haulmont.cuba.gui.components.LookupField;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.xml.layout.ComponentsFactory;
import com.company.zeusv2.entity.Annotation;

import javax.inject.Inject;
import java.util.*;

public class AnnotationBrowse extends AbstractLookup {

    @Inject
    private DataGrid<Annotation> AnnotationFieldDataGrid;
    @Inject
    private ComponentsFactory componentsFactory;
    @Inject
    private CollectionDatasource<Annotation, String> annotationsDs;
    @Inject
    private LookupField typeSearch;

    @Override
    public void init(Map<String, Object> params) {
        typeSearch.setOptionsDatasource(annotationsDs);
    }
}

Suggestions?

Regards,
Sanchit

Hi Sanchit,
I’m afraid you misunderstand what “distinct” means in this case. As the query loads instances, not separate attributes, it prevents from getting same instances of entities. But if different instances have the same value in an attribute that is used in the list of options, distinct in the query cannot help.
Do these instances mean really the same for your application? If so, why are you creating them in the first place?