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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With