Wednesday, November 17, 2021

[SOLVED] Does Hibernate allow writing of String/Type Safe SQL?

Issue

Does Hibernate allow the writing of String/Type SQL , similar to Entity Framework /w Linq, where a person can write proper Sql, and any misspelled SQL Table, Column names will be shown at compile time instead of run execution time?

create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
      .from(AUTHOR)
      .join(BOOK).on(AUTHOR.ID.equal(BOOK.AUTHOR_ID))
      .where(BOOK.LANGUAGE.eq("DE"))
      .and(BOOK.PUBLISHED.gt(date("2008-01-01")))
      .groupBy(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
      .having(count().gt(5))
      .orderBy(AUTHOR.LAST_NAME.asc().nullsFirst())
      .limit(2)
      .offset(1)

Solution

As requested I'm adding my comment as an answer.

Id depends on how you are creating your query, If you are using JPA/Native queries, no, if you are using criteria (with only entity objects), then yes.

Here is an example of using Criteria with just entity objects: https://stackoverflow.com/a/36648488/460557

Although if you are using Criteria APIs that you need to pass in Strings, then the answer is also no.



Answered By - Jorge Campos