Data retrieval can be done using NamedQuery, Criteria, or a simple Hibernate query mechanism as illustrated in the previous sections. Here are the best practices:
- Criteria can be used to dynamically create queries at runtime. The usage of Criteria for static queries should be avoided due to the disadvantages, such as cost, associated with translating a JPQL query to SQL every time it is accessed.
- NamedQuery allows queries to be accessed using alias names while making it easy to manage queries. NamedQuery is the preferred choice for static queries.