Entity commit fails due to an abnormal double upsert query (UPDATE then INSERT)

Hi,
I have an issue with my Jmix application.
I have built a screen whose aim is to allow the logged in User to edit their personal data (username / first name / last name / e-mail / so on and so forth).
It has been designed as an editor screen, which fetches the data about the logged in user with the following code:

@Autowired
private CurrentAuthentication currentAuthentication;

@Autowired
private InstanceLoader<User> editorDl;


@Install(to = "editorDl", target = Target.DATA_LOADER)
protected User editorDlLoadDelegate(LoadContext<User> loadContext) {
    return (User)currentAuthentication.getUser();
}

When I click the Save button, it tells me that the username field has been left empty, hence the saving process failed.
After debugging a bit, I discovered that Jmix, under the hood, when I click the Save and close button, performs the UPDATE query in a correct way, then it performs an INSERT INTO query in the same table, but committing an User entity whose all fields are null.
What could the problem be? How could I solve it?

Of course, don’t hesitate to ask if you need more clarifications.

Hi Paolo,

I suspect that your screen extends StandardEditor, but you don’t pass any entity to it to edit. So the editor creates a new instance when initializes.

I’d recommend overriding the setupEntityToEdit() instead of loader delegate:

@UiController("CurrentUserEdit")
@UiDescriptor("current-user-edit.xml")
@EditedEntityContainer("userDc")
public class CurrentUserEdit extends StandardEditor<User> {

    @Autowired
    private CurrentAuthentication currentAuthentication;

    @Override
    protected void setupEntityToEdit() {
        setEntityToEdit((User) currentAuthentication.getUser());
        super.setupEntityToEdit();
    }
}

So you provide the current user as from the outside, then the loader will reload it from the database and show on the screen. Saving should work correctly too.