Blog

Querydsl powered Vaadin persistence

By  
Timo Westkämper
·
On Feb 11, 2014 7:03:00 AM
·

Querydsl provides an API to write database queries in a fluent, compact and typesafe way. To strengthen Vaadin’s focus on the UI layer, a powerful persistence tool could help. What would happen if you tried to combine Querydsl with Vaadin, the de-facto way to write modern client-side web applications in Java? In this blogpost I try to provide an answer to this question.

I took the Vaadin JPAContainer addon as an integration point . The JPAContainer provides a collection like abstraction on top of JPA entities with filtering and sorting. The problem in the API is that all references to properties happen via Strings which is cumbersome and errorprone. In addition to this, the construction of filters is quite verbose and the syntax is very different from the resulting database queries.

To integrate Querydsl, I extended the JPAContainer and added typesafe wrappers for the methods that took String based property references or Filter definitions. Instead I used Querydsl types on the outer level and converted them into the internal formats.

Here are some syntax comparisons

Filtering

container.addContainerFilter(new Equal("firstName", "Hello"));
container.addContainerFilter(new Equal("lastName", "World"));


and after

QPerson person = QPerson.person; // Querydsl generated type
container.addContainerFilter(person.firstName.eq("Hello"));
container.addContainerFilter(person.lastName.eq("World"));

Filtering and sorting

container.addContainerFilter(new Equal("firstName", "Hello"));
container.sort(new Object[] { "firstName" }, new boolean[] { true });

and after
 

container.addContainerFilter(person.firstName.eq("Hello"));
container.sort(person.firstName.asc());


Complex filter example

container.addContainerFilter(new Or(new Equal("firstName", "Hello"),
                             new Equal("lastName", "World")));

after

container.addContainerFilter(person.firstName.eq("Hello")
                             .or(person.lastName.eq("World")));


As you can see from the code examples, we now have some fluent, compact and typesafe code. Instead of lots of explicit object creations, a fluent API with autocomplete is used. A similar approach is also used by Spring Data to integrate Querydsl and has been battle proven in thousands of projects.

The source code of this integration exercise is available here https://github.com/mysema/vaadin-querydsl-prototype. The supported Querydsl operations are and, or, not, like, eq, ne, isNull, startsWith, startsWithIc, contains, between, lt, gt, loe and goe.

Vaadin and Mysema planned this experiment to find an innovative way of how
to extend the Vaadin platform with Querydsl.

This blogpost and the related prototype integration scratch merely the surface of what cool things are possible when you combine Vaadin and Querydsl.

What do you think about this approach? Does it ease your Vaadin persistence issues? Do you feel that a different approach could work better?

 

Timo WestkämperTimo Westkämper is an experienced Java architect and the father of the Querydsl framework. You can follow him on twitter – @timowest