Enterprise Java

Vaadin Tip: Lazy Loading and Item Identity

Lazy Loading

When using grids, trees or any other of multi-valued component with Vaadin you often want to display data from a database table and typically you have more than a few rows in the database. In this case loading thousands or even millions of records don’t make sense and would be a huge performance problem. For this use case Vaadin provides lazy loading using a CallbackDataProvider.

To create a CallBackDataProvider you must implement a CountCallback and a FetchCallback. The CountCallback is used to provide the total number of records. And the FetchCallback is used for paging. Both methods receive a Query object that contains filter, sorting, offset and limit.

In this example you can see how to use offset and limit.

DataProvider<Employee, Void> dataProvider = new CallbackDataProvider<>(
                query -> employeeRepository.findAll(query.getOffset(), query.getLimit()),
                query -> employeeRepository.count()

Item Identity

In a Grid or the DataProvider there are methods that are using an item:


Ever wondered how Vaadin is finding the right item in the underlying data structure? No surprise – it uses equals(). But what if you can’t control how equals() is implemented? For example if the Class that you use in the Grid is generated directly from the database tables like jOOQ does?

No worries! Vaadin provides another constructor to create a CallbackDataProvivder

As a third parameter you pass a ValueProvider that is responsible to return a unique identifier. In the example this is the ID of the Employee.

DataProvider<Employee, Void> dataProvider = new CallbackDataProvider<>(
                query -> employeeRepository.findAll(query.getOffset(), query.getLimit()),
                query -> employeeRepository.count(),

What’s next?

Never heard of Vaadin? Stay tuned there will be a Vaadin introduction coming soon!

Published on Java Code Geeks with permission by Simon Martinelli, partner at our JCG program. See the original article here: Vaadin Tip: Lazy Loading and Item Identity

Opinions expressed by Java Code Geeks contributors are their own.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Simon Martinelli

Simon Martinelli is a passionate Java, performance optimization and application integration expert and an active member of the Java community process (JSR-352 Java Batch and JSR-354 Money and Currency API). He is the owner of 72 Services LLC and an adjunct professor at Berne University of Applied Science in Switzerland, teaching software architecture and design and persistence technologies
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button