Where to test JPQL queries

Back in Cuba platform I used the JMX console and got a persistence object and i was able to load and test JPQL queries from within the admin screen. How can I do the same with JMIX?
thank you.

You can do even better in your project.

Create a simple screen with SourceCodeEditor component:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://jmix.io/schema/ui/window"
        caption="msg://queryTestScreen.caption">
    <layout expand="codeField" spacing="true">
        <sourceCodeEditor id="codeField" width="100%"/>
        <comboBox id="entityComboBox" caption="Entity"/>
        <button id="testBtn" caption="Test"/>
    </layout>
</window>

Add code completion and data loading to it:

@UiController("QueryTestScreen")
@UiDescriptor("query-test-screen.xml")
public class QueryTestScreen extends Screen {

    @Autowired
    private SourceCodeEditor codeField;
    @Autowired
    private JpqlUiSuggestionProvider jpqlUiSuggestionProvider;
    @Autowired
    private DataManager dataManager;
    @Autowired
    private ComboBox<Class<?>> entityComboBox;
    @Autowired
    private MetadataTools metadataTools;

    @Subscribe
    public void onInit(InitEvent event) {
        codeField.setSuggester((source, text, cursorPosition) -> {
            if (codeField.getValue() == null) {
                return Collections.emptyList();
            }
            return jpqlUiSuggestionProvider.getSuggestions(
                    codeField.getValue(),
                    cursorPosition - 1,
                    codeField.getAutoCompleteSupport()
            );
        });

        Map<String, Class<?>> entities = new TreeMap<>();
        for (MetaClass metaClass : metadataTools.getAllJpaEntityMetaClasses()) {
            entities.put(metaClass.getName(), metaClass.getJavaClass());
        }
        entityComboBox.setOptionsMap(entities);
    }

    @Subscribe("testBtn")
    public void onTestBtnClick(Button.ClickEvent event) {
        if (codeField.getValue() != null) {
            List<?> list;
            if (entityComboBox.getValue() != null) {
                list = dataManager.load(entityComboBox.getValue()).query(codeField.getValue()).list();
            } else {
                list = dataManager.loadValues(codeField.getValue()).list();
            }
            System.out.println("Query results: " + list);
        }
    }
}

Moreover, you can add a user selection field and test query execution for different roles, by wrapping dataManager.load() with System Authentication.

2 Likes