Create Datasource in AbstractWindow

How can I create a Datasource in the AbstractWindow controller opened through openWindow with params.
I have tried to create a datasource through the xml file. Got null pointer exception.
Have tried to create datasource in Java code, no success.

I need a datasource that can pickup an WorkOrder selected from a table then send by openWindow("",params)
I have the UUID of the selected Entity but can’t look it up in the datasource. getItem(UUID) in DS is null.

Here is the code for AbstractWindow controller


@Inject
    private CollectionDatasource<ArbeidsOrdre, UUID> arbeidsOrdreDs;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);

        ArbeidsOrdre ao = arbeidsOrdreDs.getItem((UUID) params.get("uuid"));
        
    }

And here is the code from the browser view


public void onMeldOprButtonClick(Component source) {
        ArbeidsOrdre arbeidsOrdre = arbeidsOrdresTable.getSingleSelected();
        Operasjoner operasjoner = arbeidsOrdre.getOperasjoner().get(0);
        Map<String, Object> params = new HashMap();
        params.put("uuid", arbeidsOrdre.getId());
        openWindow("tilbakeMeldOpr", WindowManager.OpenType.DIALOG, params);

    }

First of all, if you work in a single instance and it is already loaded in memory, use Datasource instead of CollectionDatasource. But single-instance datasources do not load entities from the database (CollectionDatasources do if you specify a query). The behaviour of entity editor screens is provided by the special controller class - AbstractEditor and by the fact that you open it using the openEditor() method passing an edited instance to it.
So for a single-instance datasource in an AbstractWindow, set the passed instance to the datasource in init() method:


ds.setItem(myEntity);

And of course in this case you should pass the whole instance from the calling screen.

Do I get related entities to, when loading data from database from CollectionDatasource?
Or do I have to create a query where I join the other entities.

I have tried with Data manager and here I got the instance and related entities by lookup pr view.

Not sure I understand your question, but in general to load related entities you should specify an appropriate view - whether to the CollectionDatasource or to the DataManager.

A post was split to a new topic: How to create datasource programmatically