Hibernate using small Notes

Source: Internet
Author: User

In the process of using hibernate development, encountered in the scene in the condition query, the number of query conditions more than 1w or even higher (and do not discuss whether this method is reasonable), in the generation of HQL, the execution of the query when the list () method is found to be particularly slow. After the inquiry from the net, learned that:

Hibernate is the first to use its own database manipulation language-HQL
Hibernate parsing HQL's compiler parser is using the ANTLR parser
Although HQL is a database manipulation language, it is easy to write and use, essentially converting to SQL language to execute on the database.
Hibernate is mainly used in the conversion of ANTLR,ANTLR is the Java written lexical analyzer and parser, the function is very powerful.
ANTLR converts the HQL into SQL statements and then actually executes the SQL statement.
The format of the file name for the ANTLR is the *.G format, and the Hibernate jar contains 3 syntax definition files.
Hibernate contains 3 syntax definition files:
1. HQL.G defines lexical analysis and parsing and interprets HQL as a hql abstract syntax tree (AST).
2. HQL-SQL.G transforms the HQL ast into a SQL AST, decoupling the build module from Hibernate.
3. SQL-GEN.G generates the SQL language from the SQL AST.

Detailed view of the call procedure for CreateQuery () in Hibernate
An instance of the query plan Hqlqueryplan is obtained through the Queryplancache Gethqlqueryplan () method, which is mainly called the compile method of Querytranslator and compiles the HQL statement. The docompile of the inheriting class Querytranslatorimpl in Querytranslator observes this process:
PHASE 1:parse the HQL into an AST.
PHASE 2:analyze the HQL ast, and produce an SQL ast.
PHASE 3:generate the SQL.

Then locate debug to find that it takes too long to execute the Docompile method in the Querytansalatorimpl class:

The original HQL statement is too long, ANTLR to HQL to compile into SQL process will consume a long time. Then we realized the query by using the criteria condition query in hbernate, and the efficiency was improved obviously.

Criteria is a unique query method for hibernate, an object-oriented query that allows developers without the SQL Foundation to use hibernate to manipulate the database accurately.

HQL is another kind of data query language outside the Java language level, and SQL is very close, universal, easy to use by SQL experience, need syntax parsing. Use when considering portability . The Criteria API is an interface within the Java language that does not require parsing , directly manipulating underlying objects, and writing query code without commonality. Performance is optimized for maximum use.

Also, using native SQL can improve efficiency.

Hibernate using small Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.