Back

Querydsl powered Vaadin persistence

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

Comments
I would love to see a JPA-backed container which was filterable using QueryDSL. I have, however been unable to get the performance I would like out of JPAContainer. LazyQueryContainer is another potential integration point you might wish to consider. I will have a look at doing this myself.
Posted on 3/31/14 9:47 AM.
You should check out the example application I wrote to this article:
http://jaxenter.com/top-3-jpa-productivity-boosters-for-java-ee-developers-querydsl-116135.html

It is using QueryDSL via and EBJ-Spring Data combination and does filtering as well. You can naturally drop the Spring Data off the combination if you want to use just QueryDSL. You can combine this really easily to lazy loading features in Viritin (MTable, LazyComboBox, LazyList).
Posted on 4/22/15 3:52 PM in reply to Will H Temperley.