Select grid row after refreshing data

I’m trying to select an item on the grid right after saving it to the database however no selection is being made, what would be the correct way of doing this?

//Write item to database
binder.writeBean(this.transaction);

//Clear selection and refresh data
grid.select(null);
grid.getDataProvider().refreshAll();

//Select newly added database item.
grid.select(this.transaction);```

Do you have equals and hashCode implemented in the Transaction class?

Hi Simon, yes I do:

@Override
    public int hashCode() {
        if (id != null) {
            return id.hashCode();
        }
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Transaction)) {
            return false; // null or other class
        }
        Transaction other = (Transaction) obj;

        if (id != null) {
            return id.equals(other.id);
        }
        return super.equals(other);
    }```

and how do you set the items?

//View
grid.setItems(query -> transactionService.list(
                PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query)))
                .stream());

//TransactionService
public Page<Transaction> list(Pageable pageable) {
        return repository.findAll(pageable);
    }

But only writeBean does not insert the record to the database

Ah yeah, there is more code, which was generated from the Vaadin app generator and I replace with my data class, which happens to be “Transaction”.

save.addClickListener(e -> {
            try {
                if (this.transaction == null) {
                    this.transaction = new Transaction();
                }

                binder.writeBean(this.transaction);
                transactionService.update(this.transaction);
                clearForm();
                refreshGrid();
                grid.select(this.transaction);

                Notification.show("Transaction saved.");
            } catch (ValidationException validationException) {
                Notification.show("An exception happened while trying to store the transaction details.");
            }
        });
    private final BeanValidationBinder<Transaction> binder;
    binder = new BeanValidationBinder<>(Transaction.class);
    binder.bindInstanceFields(this);
    binder.writeBean(this.transacao);

By default saving just refreshes the grid and I added the line to select the just added transaction afterwards.

Could it be that grid.getDataProvider().refreshAll() is running async thus the selection runs first?

You could try to select it in the setItems callback

Will give it a try, that code runs when the view is loaded whereas the refresh when the save button is pressed.

Check the grid selection mode, e.g. grid.setSelectionMode. If it is set to NONE, you will not see the selection even if you set it explicitly.