Showing posts with label criteria. Show all posts
Showing posts with label criteria. Show all posts

2010-05-13

Querydsl user experiences

I just found this blog post describing the experiences of one Querydsl HQL user : JPA 2 Criteria

The blog post mentions the following benefits of Querydsl in comparison to JPA 2 Criteria :
* conciseness and readability (also discussed here)
* extendability of Query classes (also discussed here)
* support for other backends (JDO, SQL, Lucene etc)

Here are some other Querydsl user comments

"...We have applied Querydsl to the articial playground project and to a realworld application, LeanCast. Querydsl has been easy to get running, and easy to write compact and readable queries in. Integration in an existing project has been as painless as it could be, and we have had almost no problems."

"...Where we had problems, they were resolved in manner of hours by the Querydsl developers, which both seem dedicated and got a company backing the development. The API is complete, making all queries we have tried to express possible. When we had a request for a simplifying addition to the API, it was willingly accepted. ..."

Stein Magnus Jodal

"...Querydsl rocks! It's the best thing since sliced bread for data access in Java."

Alex Besogonov

"...Using querydsl you keep the reference to your HibernateQuery object and you can apply as many joins, wheres etc as you can, in any order, so that you can get a totally mutable hql query without ugly string concatenation. "

Lukasz Wisniewski

We at Mysema hope that you like Querydsl as much as we do ;)

2010-04-13

Querydsl as an alternative to the JPA 2 Criteria API

Querydsl is our flagship Open Source project for type-safe queries in JDO, JPA, SQL and other querying languages.

Last weekend I posted a thread in our Mysema Source Forum related to the verbosity of JPA 2 Criteria queries.

The thread presents JPA 2 and Querydsl queries side by side. Just that, not more. Here is one query comparison example.

The use case of this example is to query for single men and women as an cartesian view. At first the query expressed via the JPA 2 Criteria API :

CriteriaQuery query = builder.createQuery();
Root<Person> men = query.from( Person.class );
Root<Person> women = query.from( Person.class );
Predicate menRestriction = builder.and(
    builder.equal( men.get( Person_.gender ), Gender.MALE ),
    builder.equal( men.get( Person_.relationshipStatus ),
RelationshipStatus.SINGLE ));
Predicate womenRestriction = builder.and(
    builder.equal( women.get( Person_.gender ), Gender.FEMALE ),
    builder.equal( women.get( Person_.relationshipStatus ),
RelationshipStatus.SINGLE ));
query.where( builder.and( menRestriction, womenRestriction ) );

and now via Querydsl

JPAQuery query = new JPAQuery(em);
QPerson men = new QPerson("men");
QPerson women = new QPerson("women");
query.from(men, women).where(
      men.gender.eq(Gender.MALE),
      men.relationshipStatus.eq(RelationshipStatus.SINGLE),
      women.gender.eq(Gender.FEMALE),
      women.relationshipStatus.eq(RelationshipStatus.SINGLE));    

The guys from the JPA group decided that fluent expression construction is not an option so a CriteriaBuilder for criteria construction was needed. CriteriaBuilder contains methods for all possible operation constructs, not grouped in any way.

Compare this with Querydsl, which has a dynamic query model with a fluent API, which supports IDE autocomplete and a much faster and compact way to write queries.

Also the distinction of paths, joins and the metamodel doesn't really make query construction easy.

A root path for the Person type is constructed like this in JPA 2 :

Root<Person> men = query.from( Person.class );

In Querydsl you just create an instance of the metatype for Person :

QPerson men = new QPerson("men");

In most cases though you can use the default instance of the metatype like this :

QPerson men = QPerson.person;

And to refer to the gender property of the Person path :

men.get(Person_.gender)

whereas in Querydsl you just use a field of the metatype instance :

men.gender

The dynamic metatypes of Querydsl combine a type-safe representation of the query domain, path references and operation construction in a fluent and concise way.

In addition to JPAQL and HQL Querydsl supports JDOQL, SQL, RDF, Lucene, Hibernate Search and Java collections.

We at Mysema use Querydsl in all our own projects with multiple backends and are 100% happy with it. ;)