Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Criteria API and Query DSL

Using the canonical meta model in JPA 2 provides very strong type safety. I am not sure about querydsl though. Which is more type safe and why?

like image 259
M.K. Avatar asked Dec 19 '14 20:12

M.K.


People also ask

What is a query DSL?

Querydsl is an extensive Java framework, which allows for the generation of type-safe queries in a syntax similar to SQL. It currently has a wide range of support for various backends through the use of separate modules including JPA, JDO, SQL, Java collections, RDF, Lucene, Hibernate Search, and MongoDB.

What is the criteria API and what is it used for?

The Criteria API is used to define queries for entities and their persistent state by creating query-defining objects. Criteria queries are written using Java programming language APIs, are typesafe, and are portable. Such queries work regardless of the underlying data store.

What is Criteria API in Hibernate?

Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like "search" screens where there is a variable number of conditions to be placed upon the result set. The Session is a factory for Criteria.

Is Criteria API deprecated?

The Criteria API allows us to build up a criteria query object programmatically, where we can apply different kinds of filtration rules and logical conditions. Since Hibernate 5.2, the Hibernate Criteria API is deprecated, and new development is focused on the JPA Criteria API.


1 Answers

Query DSL is typesafe it's actually one of the main reasons to use QueryDSL and one of the problems it tries to solve. You create your metamodel and it enables you to effevtively use it with JPA, JDO, SQL ..

The reason why you may want to consider to use it instead of the JPA may be the more easier expression construction or that it plays well with Spring Data. Or maybe you want to access a MongoDB..

In terms of type safety it doesn't matter which one you use imho. As often the Standard (JPA) picks the cherrys from projects (e.g. CDI, JSR, ..)

edit: their own principles state Type safety is the core principle of Querydsl. Queries are constructed based on generated query types that reflect the properties of your domain types. Also function/method invocations are constructed in a fully type-safe manner.

like image 186
wgitscht Avatar answered Sep 30 '22 12:09

wgitscht