How is the correct way to save a CheckBox to JPA

How is the correct way to save a Vaadin CheckBox to JPA’

I’m looking to find a Vaadin example but I didn’t find any using of Checkbox to JPA at all.

I try this but is work only if I click one time on a checkbox, if click again on the same box is breaks off

private Grid createGrid() {
    grid = new Grid<>();
    ListDataProvider<User> dataProvider = DataProvider.ofCollection(userRepository.findAll());
    grid.setDataProvider(dataProvider);
    grid.setHeight("100%");
    grid.setMaxWidth("840px");

    grid.addColumn(new ComponentRenderer<>(this::createUserInfo)).setWidth(UIUtils.COLUMN_WIDTH_XL);
    grid.addComponentColumn(u -> {
        Checkbox c = new Checkbox();
        c.getStyle().set("font-size", "24px");
        c.setValue(u.isAnwesend());
        c.addValueChangeListener(click -> {
            changeIsAnwesend(u);
        });

        return c;
    })
            .setFlexGrow(0)
            .setWidth(UIUtils.COLUMN_WIDTH_XS);

    grid.addThemeVariants(GridVariant.LUMO_NO_ROW_BORDERS);

    return grid;
}

private void changeIsAnwesend(User user) {
    user.setAnwesend(!user.isAnwesend());
    //grid.setSelectionMode(Grid.SelectionMode.SINGLE).select(user);
    userRepository.save(user);
}

This are the errors:

*org.springframework.orm.ObjectOptimisticLockingFailureException: Object of class [com.softwareaustria.backend.data.entity.User]

with identifier [21]
: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.softwareaustria.backend.data.entity.User#21]

Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.softwareaustria.backend.data.entity.User#21]

Before I begin to investigate in Hibernate I ask what is the correct and usual way to save Checkbox to JPA in Vaadin.

Thanks a lot!

The solution was to change method parameters to (User user, Grid grid) and add after userRepository.save(user);

grid.setItems(userRepository.findAll());