Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm the guy who wrote the Hibernate 3 query translator back in 2004, so I know something about how it works.</p> <p>Criteria, in theory should have less overhead than an HQL query (except for named queries, which I'll get to). This is because Criteria doesn't need to parse anything. HQL queries are parsed with an ANTLR-based parser and then the resulting AST is turned into SQL. However, with HQL/JPAQL you can define named queries, where the SQL is generated when the SessionFactory starts up. In theory, named queries have less overhead than Criteria.</p> <p>So, in terms of SQL-generation overhead we have:</p> <ol> <li>Named HQL/JPAQL Query - SQL generation happens only once.</li> <li>Criteria - No need to parse before generating.</li> <li>(non-named) HQL/JPAQL Query - Parse, then generate.</li> </ol> <p>That said, <strong><em>choosing a query technique based on the overhead of parsing and SQL generation is probably a mistake</em></strong> in my opinion. This overhead is typically very small when compared to performing a real query on a real database server with real data. If this overhead does actually show up when profiling the app then <em>maybe</em> you should switch to a named query.</p> <p>Here are the things I consider when deciding between Criteria and HQL/JPAQL:</p> <ul> <li>First, you have to decide if you're <em>OK</em> with having a <strong>dependency on Hibernate-proprietary API</strong> in your code. JPA doesn't have Criteria.</li> <li><strong>Criteria is really good at handling many optional search parameters</strong> such as you might find on a typical web page with a multi-parameter 'search form'. With HQL, developers tend to tack on where clause expressions with StringBuilder (<strong>avoid this!</strong>). With Criteria, you don't need to do that. Hardik posted similar opinions.</li> <li>HQL/JPAQL can be used for most other things, because the code tends to be smaller and easier for developers to understand.</li> <li>Really frequent queries can be turned into named queries if you use HQL. I prefer to do this later, after some profiling.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload