1

I'm thinking of porting an Hibernate criteria from an old DAO layer to either a JPA criteria or a QueryDSL one.

Since I've never used any of these two, I wonder which API I should use...

Here is the Hibernate criteria:

public Page<ElearningSubscription> findWithPatternLike(String searchPattern, int pageNumber, int pageSize) {
    Criteria criteria = getSession().createCriteria(getPersistentClass(), "es");
    criteria.createAlias(DB_TABLE_USER_ACCOUNT, "u", CriteriaSpecification.INNER_JOIN);
    Conjunction conjunction = Restrictions.conjunction();
    String pattern = "%" + searchPattern + "%";
    Criterion firstname = Restrictions.ilike("u.firstname", pattern);
    Criterion lastname = Restrictions.ilike("u.lastname", pattern);
    Criterion email = Restrictions.ilike("u.email", pattern);
    Disjunction disjunction = Restrictions.disjunction();
    disjunction.add(firstname).add(lastname).add(email);
    if (searchPattern.contains(" ")) {
        String[] pieces = searchPattern.split(" ");
        if (pieces[0] != null) {
            Criterion firstnameBis = Restrictions.ilike("u.firstname", pieces[0]);
            disjunction.add(firstnameBis);
        }
        if (pieces[1] != null) {
            Criterion lastnameBis = Restrictions.ilike("u.lastname", pieces[1]);
            disjunction.add(lastnameBis);
        }
    }
    conjunction.add(disjunction);
    OrderList orderList = new OrderList().add(Order.asc("u.firstname")).add(Order.asc("u.lastname")).add(Order.asc("u.email")).add(Order.desc("es.subscriptionDate"));
    Page<ElearningSubscription> page = getPage(pageNumber, pageSize, criteria, orderList);
    return page;
}

Thanks for any guidance.

Kind Regards,

Stephane Eybert

Stephane
  • 11,836
  • 25
  • 112
  • 175

1 Answers1

0

JPA 2 Criteria is an official standard, but Querydsl is superior in the following aspects

  • easier and less verbose syntax
  • customizable code generation
  • supports multiple backends

This answer is biased, since I am involved in the Querydsl development.

Timo Westkämper
  • 21,824
  • 5
  • 78
  • 111
  • Hi Timo, thanks, I just commented on one of your blog articles :-) I was wondering if QueryDSL was typesage in a Spring Data environment. I reckon it is..? I will try that route then. Now will hunt for a tutorial. Cheers ! – Stephane Aug 12 '14 at 06:13
  • Timo, if you had a few minutes, I've been having a little stone in my shoe for a few days now, a different issue which evades me... http://stackoverflow.com/questions/25221495/hibernate-and-jpa-error-duplicate-import-on-dependent-maven-project – Stephane Aug 12 '14 at 07:46
  • Found that QueryDSL is typesafe http://manuel-palacio.blogspot.fr/2011/01/compact-queries-with-querydsl.html :-) – Stephane Aug 12 '14 at 09:40
  • Ys, Querydsl is typesafe and the given answer looks sufficient for your other SO issue. – Timo Westkämper Aug 12 '14 at 15:06
  • 1
    I wrote a query of medium complexity in both frameworks for checking them out. JPA 2 Criteria was a lot of code using many different classes. It was cumbersome to write and rather hard to read. QueryDSL only requires you to create a `JPAQueryFactory` passing the `entityManager` instance, then you can start to write an easy to read query very fluently. So we decided to go for QueryDSL. – sorrymissjackson Aug 12 '15 at 07:47