Search code examples
sqlhibernatejpaquerydsl

What exactly does type-safe queries means?


While going through JPA, QueryDSL they all have included the concept of type-safe queries. But what exactly is it? According to blogs/articles, I guess it is a feature of JPA/QueryDSL that validates their parameter type while making queries. And any thing wrong with the query would show up in compile time instead of run time. Am I right on this? Is it only for this or am I missing something here?


Solution

  • QueryDSL and Criteria are libraries that try to enforce the data type validation when you assemble a query in JPA.

    Just because JPQL is just a query that will be only verified on execution time, it's easy to write an invalid query that is not detected by the compiler. Of course, it's faster to fix things while you're coding than while you're running the app.

    Of course, neither QueryDSL or Criteria can protect your query from all kinds of errors (data type related or not) but it's safer than JPQL.

    On the other hand, writing queries in JPQL can be much easier and faster. Always the tradeoff :-)