-->

Are Hibernate Named Queries precompiled in the tru

2020-05-20 09:18发布

问题:

Pre-compiled queries are compiled and cached in advance by DB vendor (like oracle,sql server etc) so that they can be faster for successive calls like prepared statement.

In Hibernate Named queries are said to be pre-compiled at web server start up. Does it means all queries are fired at server startup itself so that they can be pre-compiled by DB vendor or pre-compilation has different meaning in hibernate context?

回答1:

  1. The Hibernate Named queries are expressed in an object query language (JPQL or HQL), so Hibernate needs to translate them to SQL first. The named queries are stored in a NamedQueryRepository and each query is represented by a NamedQueryDefinition.

    Because the user can dynamically add filters, query result limits, locks and hints, Hibernate cannot pre-compile the HQL/JPQL until execution time.

  2. Hibernate also uses a PreparedStatement for each SELECT and DML statement so you can also get database statement precompilation if the JDBC driver supports it and doesn't emulate the prepare phase by multiplexing the prepare and the execute in a single database request (e.g. MySQL, PostgreSQL).