2010-06-22

Typesafe queries for DataNucleus

The guys from DataNucleus are now working actively on getting typesafe queries working natively in the Access Platform and later as part of the JDO standard.

The following blog post shows the direction and gives a concrete query example :

QCustomer customer = QCustomer.customer;
Query<Customer> q = pm.newTypesafeQuery(customer);
Customer bob = q.filter(customer.firstName.eq("Bob"))
     .unique(true).execute();

What about queries which return customer.firstName results?

Using the type parameter in the query there are the options of encoding the source type or the projection in the query.

The example is a bit vague, but could be transformed into the following forms

Projection as argument

QCustomer customer = QCustomer.customer;
Query q = pm.newTypesafeQuery(customer);
Customer bob = q.filter(customer.firstName.eq("Bob"))
     .unique(true).execute(customer);

What about list typed results? Do we get this type-safe?

Unique flag implicit via projection method

QCustomer customer = QCustomer.customer;
Query q = pm.newTypesafeQuery(customer);
Customer bob = q.filter(customer.firstName.eq("Bob"))
     .unique(customer);

The last example is closest to the Querydsl projection usage, and is also the most compact.

These examples manage without a type parameter, since the projection type is given in the end.

2010-06-08

Querydsl usage in Cyclos

Until now Querydsl has been mostly used in smaller projects outside of Mysema. Cyclos is one of the larger adopters of Querydsl.

Cyclos is an open source system for complementary currencies and is used in several projects with methodologies like C3, LETS, time banks and Barter's. See their homepage for more information. From their SourceForge download statistics can be seen that there are a large number of downloads for a server application in this area.

Cyclos 4

The current stable version of Cyclos is 3.5. Beta versions of 3.6 with numerous improvements are already available. Cyclos 4 was started a year ago. Some new features needed major refactorings to the old code so a new branch with new technologies was started. Struts, Hibernate and Tomcat were replaced with GWT, EJB3 (using Hibernate), Glassfish and Querydsl.

The goal was to make everything configurable: from accounts, to custom fields and localization. Beside the usual application localization even proper data localization is being implemented. Configuration entities have, for example, the name in each active language.

Cyclos is being produced by two teams of programmers, one in Brazil and one in Uruguay, being part of the respective national social trade organisation: STRO Uruguay and Instrodi. Luis Fernando Planella Gonzalez from the Brazilian team is the person behind the Querydsl adoption and has been communicating actively with the Querydsl maintainers to make the adoption as smooth as possible.

Querydsl usage

The Querydsl usage in Cyclos is very advanced. The Querydsl HQL module is used in conjunction with the JPA 2 API as an replacement for the JPA 2 Criteria API. JPA 2 Criteria were found to be too verbose. See the following post for some JPA 2 Criteria vs Querydsl comparisons.

Instead of using the the default query classes for JPA 2 provided by Querydsl, custom query and subquery classes were created to customize Querydsl as good as possible to the demands of Cyclos 4 development. The customizations are Entity to DTO transformations in the query projection, convenience methods for paging and some Hibernate specific optimizations.

The sources of Cyclos 4 are accessible via Subversion from this location : https://svn.berlios.de/svnroot/repos/cyclos.