Retrieving persistent objects from the database is one of the most interesting parts of working with Hibernate.
Hibernate provides the following ways to get objects out of the database: ■ Navigating the object graph, starting from an already loaded object, by accessing the associated objects through property accessor methods such as a Address()City(), and so on.
The default fetch plan and fetching strategy is the plan and strategy that applies to a particular entity association or collection.
■ The ability to retrieve only properties of an entity or entities, without the overhead of loading the entity itself into the persistence context. ■ Aggregation with group by, having, and aggregate functions like sum, min, and max/min.
This is sometimes called a report query; it is more correctly called projection. ■ Outer joins when retrieving multiple objects per row.
Before we look at the fetch plan options and fetching strategies, we’ll give you an overview of the retrieval methods.
(We also mention the Hibernate caching system sometimes, but we fully explore it later in this topic.) You saw how objects are retrieved by identifier earlier in the previous topic, so we won’t repeat it here.
Object state synchronization is the job of the persistence manager, not the developer.
But, as we’ve shown in the previous topic, HQL and JPA QL support direct bulk operations for updating, deleting, and inserting, if required by the use case (mass data operations).
Let’s go straight to the more flexible query options, HQL (equivalent to JPA QL) and Criteria. The Hibernate Query Language is an object-oriented dialect of the familiar database query language SQL.
HQL bears some close resemblance to ODMG OQL, but unlike OQL, it’s adapted for use with SQL databases and is easier to learn (thanks to its close resemblance to SQL) and fully implemented (we don’t know of any OQL implementation that is complete).
Each retrieval method may use a different plan and strategy—that is, a plan that defines what part of the persistent object network should be retrieved and how it should be retrieved.
Your goal is to find the best retrieval method and fetching strategy for every use case in your application; at the same time, you also want to minimize the number of SQL queries for best performance.
■ Native SQL queries, including stored procedure calls, where Hibernate still takes care of mapping the JDBC result sets to graphs of persistent objects.