Retrieving objects from the database is one of the most interesting parts of Hibernate.
Hibernate provides the following ways to get objects out of the database:
- Retrieval by identifier, the most convenient method when the unique identifier value of an object is known.
- The Hibernate Query Language (HQL), which is a full object-oriented query language. The Java Persistence query language (JPA QL) is a standardized subset of the Hibernate query language.
- The Hibernate Criteria interface, which provides a type-safe and object-oriented way to perform queries without the need for string manipulation. This facility includes queries based on example objects.
- Native SQL queries, including stored procedure calls, where Hibernate still takes care of mapping the JDBC result sets to graphs of persistent objects.
Retrieval by identifier:
This retrieval by identifier results in a single (or possibly several, if inheritance or secondary tables are mapped) SQL statement that retrieves an Object. In order to use retrieval by identifier you have to search your object by its primary key.
For example, supposing you want to search the User with id 100:
User user = (User) session.load(User.class, new Long(100)); Another way to retrieve the same object is by means of the session.get(User.class, identifier) method: User user = (User) session.get(User.class, new Long(100));
What is the difference between session.load and session.get ? according to the Hibernate documentation if load() can’t find the object in the cache or database, an exception is thrown. The load() method never returns null. The get() method returns null if the object can’t be found.
The load() method may return a proxy instead of a real persistent instance. A proxy is a placeholder that triggers the loading of the real object when it’s accessed for the first time; On the other hand, get() never returns a proxy. Choosing between get() and load() is easy: If you’re certain the persistent object exists, load() is a good option. If you aren’t certain there is a persistent instance with the given identifier, use get() and test the return value to see if it’s null.
Using load() has a further implication: The application may retrieve a valid reference (a proxy) to a persistent instance without hitting the database to retrieve its persistent state. So load() might not throw an exception when it doesn’t find the persistent object in the cache or database; the exception would be thrown later, when the proxy is accessed. Of course, retrieving an object by identifier isn’t as flexible as using arbitrary queries.
The Hibernate Query Language (HQL)
Most of the time, you only need to retrieve objects of a particular class and restrict by the properties of that class. For example, the following query retrieves a Customer by surname.
Query q = session.createQuery("from Customer as c where c.surname = :surname"); q.setString("surname", "Frank"); List result = q.list();
After preparing query q, you bind a value to the named parameter :fname. The result is returned as a List of Customer objects.
Hibernate Criteria interface
The Hibernate query by criteria (QBC) API allows a query to be built by manipulation of criteria objects at runtime. This lets you specify constraints dynamically without direct string manipulations:
Criteria criteria = session.createCriteria(Customer.class); criteria.add( Restrictions.like("surname", "Frank") ); List result = criteria.list();
if you need to use lots of native queries (maybe with useful db native optimizations) then you can use the createNativeQuery method as follows:
Query sqlQuery =session. createNativeQuery("select c.customer_ID, c.customer_NAME, c.customer_SURNAME from Customer c",Customer.class);