How to save screen data context


What’s the correct way to save the data context of a screen in the controller?. That is, how to invoke the WINDOW_COMMIT on the screen.
I’ve tried almost everything.commit() without sucess.


1 Like

Hi Pedro,

getDsContext().commit() should work for a screen of any type. See also DsContext.

Hi Konstantin, thanks.

I had tried that. It doesn’t seem to work with related entities.
I have a “Voyage” entity. A “Voyage” has “Passengers” .
My “Voyage” screen editor as a Button to “issue tickets” wich calls a service.
Let’s say I have two Passengers in a Voyage and open the editor. If I add a new passenger, there’s no way my service can issue three tickets without closing the screen and open it again. I still haven’t found the way.

Let me first explain how the saving of data in a screen works.

When a user clicks OK, after successful validation the screen invokes the DsContext.commit() method. DsContext gathers new and modified entities from all datasources and passes them to the DataManager.commit() method. New entities inserted, modified ones are updated in the database, all in one transaction.

When you see that something is not saved to the database as desired, first check if it really does not come to DataManager: set a breakpoint at DataManagerBean.commit(CommitContext) method and look at the CommitContext.commitInstances collection.

An entity will not be passed to DataManager.commit() if it is not contained in a datasource or if the datasource did not become modified. For example, if you add a new entity to a collection datasource using includeItem() method instead of addItem().

Another possible issue is related to collections. For example, when you save a master entity that has a one-to-many collection, it doesn’t mean that changes in the collection will be automatically saved. Only changes in the “owning” side of association (mapped with @JoinColumn) are saved automatically, so you have to pass to DataManager all changed in the collection entities separately to update their reference to the master, effectively saving the changes in the collection contents.

All these conditions are handled automatically when you work with data declaratively using browser-editor screens and datasources, but since you do something programmatically, you have to ensure all changed entities go to CommitContext. You can place entities to datasources or add them to CommitContext in a DsContext.BeforeCommitListener.

If you want us to help you in the current situation, please provide a small test project reproducing the problem.

Hello Konstantin

If my updated related entities “RelatedEntity” are in collectiondatasource “MyRelatedDs” shouldn´t
getDsContext().addBeforeCommitListener(context -> {
for (RelatedEntity re : MyRelatedDs.getItems())
in Init(Map… do the trick? Well, it does not.
How do I set a breakpoint in code? That’s one more thing I can’t do no matter how.
Getting a bit desperate here, I’m used to solve my problems and cant’t do it in Cuba, I’ve never have to resort so much to forums and other users.
You must agree that all your information above is NOT very well documented in the documentation.
I don´t know, I think I’m going to prepare you a sample project.

Thank you very much

Yes, when you add entities to commit context, they certainly go to middleware and should be saved. Perhaps the problem is not in saving. Maybe your instance has no reference to the master entity? Adding an entity to collection does not automatically set the property that links entities in the database (JoinColumn).

How do I set a breakpoint in code?

If you use IntelliJ, press Ctrl+N and type DataManagerBean. Open this class, find commit(CommitContext) method and set a breakpoint in it. Start application server from Studio, then press “Debug” button next to “localhost:8787” in IDE. When you work with the application UI, your IDE will stop on the breakpoint and show you everything.

1 Like

Those collection datasources are not nested if that’s what you mean. I pass the linked property as a Map to the create action as you explained earlier. Everything works and is commited correctly when the Ok button is pressed.

And I don’t have a “Debug” button anywhere in the IDE