问题
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:
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.
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).
来源:https://stackoverflow.com/questions/27565706/are-hibernate-named-queries-precompiled-in-the-true-sense