How to Build a Vaadin CRUD Application With Spring JPA
Build a Vaadin CRUD screen on Spring Data JPA: entity, repository, a lazy grid, a binder backed form and delete with confirmation, in one view.

A CRUD screen is the job Vaadin does best: a grid, a form and a database, with no JavaScript written by hand. This guide builds one for a Customer entity on Vaadin 25, Spring Boot 4, Spring Data JPA and JDK 25. It builds a paged, filterable grid, a validated form and a confirmed delete, and it pays attention to the parts that go wrong in real use: transaction boundaries, unique constraint errors and concurrent edits. Project setup is in Vaadin with Spring Boot.
Entity, repository, service
@Entity
public class Customer {
@Id @GeneratedValue
private Long id;
@NotBlank private String name;
@Email private String email;
// getters and setters
}public interface CustomerRepository extends JpaRepository<Customer, Long> {
Page<Customer> findByNameContainingIgnoreCase(String name, Pageable pageable);
}
@Service
public class CustomerService {
private final CustomerRepository repo;
CustomerService(CustomerRepository repo) { this.repo = repo; }
public Page<Customer> find(String filter, Pageable page) {
return repo.findByNameContainingIgnoreCase(filter, page);
}
@Transactional
public Customer save(Customer c) { return repo.save(c); }
@Transactional
public void delete(Customer c) { repo.delete(c); }
}The view never touches the repository directly, which keeps transactions where Spring expects them. The service is the seam where you add rules later, such as audit entries or uniqueness checks, without editing UI code.
The view
@Route("customers")
class CustomerView extends VerticalLayout {
private final Grid<Customer> grid = new Grid<>(Customer.class, false);
private final TextField filter = new TextField("Filter by name");
private final Binder<Customer> binder = new Binder<>(Customer.class);
private final CustomerService service;
private Customer current = new Customer();
CustomerView(CustomerService service) {
this.service = service;
grid.addColumn(Customer::getName).setHeader("Name");
grid.addColumn(Customer::getEmail).setHeader("Email");
filter.setValueChangeMode(ValueChangeMode.LAZY);
filter.addValueChangeListener(e -> grid.getDataProvider().refreshAll());
grid.setItems(q -> service.find(filter.getValue(),
PageRequest.of(q.getPage(), q.getPageSize())).stream());
grid.asSingleSelect().addValueChangeListener(e -> edit(e.getValue()));
add(filter, grid, form());
}
}The grid asks the service for one page at a time, so only the rows on screen are loaded and the table can hold millions of records. ValueChangeMode.LAZY waits for a short pause in typing before it sends the filter, which avoids a query per keystroke. The grid construction with false as the second argument disables automatic columns, so only the columns you declare appear. Lazy loading and its options are covered in Vaadin Grid lazy loading.
The form
private FormLayout form() {
var name = new TextField("Name");
var email = new EmailField("Email");
binder.forField(name).asRequired().bind("name");
binder.forField(email).bind("email");
var save = new Button("Save", e -> save());
var delete = new Button("Delete", e -> confirmDelete());
return new FormLayout(name, email, new HorizontalLayout(save, delete));
}
private void edit(Customer c) {
current = c == null ? new Customer() : c;
binder.readBean(current);
}
private void save() {
if (binder.writeBeanIfValid(current)) {
service.save(current);
grid.getDataProvider().refreshAll();
edit(null);
}
}writeBeanIfValid runs the validators and shows errors beside the fields when one fails, and it writes into the bean only when all pass. Bean validation annotations on the entity are enforced by a BeanValidationBinder, described in forms and validation with the Binder. The edit(null) call after a save resets the form for the next customer.
Delete with confirmation
private void confirmDelete() {
var dialog = new ConfirmDialog("Delete customer?", "This cannot be undone.",
"Delete", e -> { service.delete(current); grid.getDataProvider().refreshAll(); edit(null); },
"Cancel", e -> {});
dialog.open();
}A delete is the one action users regret, so a confirmation is worth its extra click. If a customer has orders, the database will refuse the delete with a foreign key error, so catch it and explain it rather than showing an error page.
Why the service layer matters
A view that calls the repository directly pushes transaction boundaries into UI code, and the Binder then writes into entities that Hibernate may still be tracking. Passing the entity through a service keeps the persistence context closed after each call. The grid receives detached objects, which is what you want for a screen that stays open for minutes. The same rule explains the lazy callback: the query object carries an offset and a limit, the service turns that into a PageRequest, and nothing is retained between requests. When the filter changes, refreshAll() discards the old pages and asks again.
Handling errors
A save can fail for reasons the Binder cannot know about, such as a unique constraint on the email. Catch the exception in the view and show a notification, instead of letting it reach a generic error page.
try {
service.save(current);
} catch (DataIntegrityViolationException ex) {
Notification.show("That email is already in use");
return;
}For a unique rule that users hit often, check it in the service before saving and report it as a field error through the Binder's validation status handler. Do both: the pre-check gives a good message, and the constraint in the database stays the authority when two users save at the same moment.
Testing the screen
The service is a plain Spring bean and tests with @DataJpaTest and a few assertions on filtering and saving. The view itself is Java, so a unit test can construct it, set the filter and assert on the grid's items without a browser. Keep one browser-level test for the main journey. Both approaches are in testing Vaadin views.
Security and data access
A CRUD screen is only as safe as the service behind it. Every method on CustomerService should check that the current user may perform the action, with method security annotations such as @PreAuthorize("hasRole('SALES')"), and not rely on hiding a button in the view. A user who cannot see the Delete button can still send the request if the server does not check, because Vaadin views run on the server but are invoked through a protocol an attacker can imitate. The route itself should also be restricted, which is covered in Vaadin with Spring Security.
Be careful with what the grid loads. A Grid<Customer> bound to an entity with lazy associations fails with a LazyInitializationException the first time a column touches one, because the service closed the session. Either map the rows to a small record with only the displayed fields, or fetch the association in the query. Records for the grid also stop sensitive fields from reaching the browser, since the data provider sends only what the columns use but the bean stays in server memory.
Performance notes
The grid's lazy provider makes two kinds of calls: a count and a page. Providing a count query on a large table costs a full scan if the filter is not indexed, so index the column you filter on. Setting setPageSize to a larger value reduces round trips but loads more rows per scroll, and the default of fifty is a reasonable middle. With ValueChangeMode.LAZY, the filter waits 400 milliseconds after the last keystroke by default, a delay you can tune for slower databases.
Layout and navigation
Put the grid and the form side by side with a SplitLayout so the user sees both at once, and add the route to a main layout with a menu so the screen is reachable. Keep the form hidden until a row is selected or a "New customer" button is pressed, which makes the empty state obvious.
Keyboard and layout polish
Two small changes make a CRUD screen feel finished: submit the form when the user presses Enter in the last field, using Button.addClickShortcut(Key.ENTER), and set the focus on the first field when a row is selected. Disable the Delete button for a new, unsaved customer, and show a notification after a successful save. These details cost minutes and remove most of the first round of user feedback.
Where a form-and-grid screen is not enough
A grid that edits in place across a hundred columns wants the Grid Pro component or a spreadsheet-style tool, not a form. For concurrent edits of one record, add a @Version field and handle the optimistic locking failure, telling the user that someone else saved first. The full APIs are in the Vaadin Grid documentation and the Spring Data JPA reference.


