This is a tutorial for first-timers in which basic query mechanics in Hibernate will be discussed.
For further examples using Hibernate, check the following tutorials:
- Hello Hibernate 4 with Eclipse Kepler: http://peace-and-code.blogspot.com/2013/10/java-hello-hibernate-4-with-eclipse.html;
- Hibernate 4 object states: http://peace-and-code.blogspot.com/2013/10/java-hibernate-4-object-states.html;
- Mapping relational concepts with Hibernate 4: http://peace-and-code.blogspot.com/2013/10/java-mapping-relational-concepts-with.html.
1. createQuery
The most basic approach for querying data in Hibernate by, first, creating a Query[1] object, then the query is executed by calling the proper methods of the class. A Query object could be created using createQuery[2], createFilter[3] or getNamedQuery[4].
A query created using createQuery must be in HQL which is a query language for Hibernate relying on the object representation of data.
In this example, first, last and isbn are variables in their respective query sentences, they are bound to the desired values using Query's setString and setLong which are part of a series of methods used to bind values to variables depending on their data types.
Generally, the query is executed by calling list on the Query object. If the query is guaranteed to always have a unique result, uniqueResult will be sufficient to execute it.
The results of the query could be iterated over directly using iterate. This method executes the query as well, but, compared to list it can have an execution overhead in some cases.
In case the results are tuples of objects, each returned tuple will be represented as an array of objects. The returned array can contain actual members of the queried persistent entities as well as the results of aggregate function calls.
2. createFilter
Hibernate offers the possibility to query persistent collections. In order to take this shortcut, the developer has to create a Query object using createFilter. The method takes the collection and a HQL query as arguments.
The from clause is usually omitted since an implicit entity class named this which refers to the persistent collection is queried through createFilter.
3. Criteria and DetachedCriteria
Instead of using explicit query sentences, the developer can use Criteria[5] objects to query an entity class.
A Criteria object is instantiated using createCriteria[6]. Criteria has a set of methods which allow to programmatically define a query. The query can include the following operations:
- definition of aliases;
- projections;
- restrictions;
- joining associate entity classes;
- use of aggregate functions;
- grouping the results;
- ordering the result set;
- subqueries.
4. createSQLQuery
Queries can be defined using SQL by building a SQLQuery object which is the return value of createSQLQuery[9]. Executing an SQLQuery returns raw data from the database. To overcome this behavior, the returned results should be mapped an entity class.
5. HQL
The Hibernate Query Language (HQL) is similar to SQL's except that HQL is object-oriented. In fact, from the user's perspective, HQL queries entity classes.
HQL supports select, from, where, order by, group by and having clauses. It also helps querying inner joins, outer left joins and outer right joins. Aggregate functions and subqueries are also part of the HQL syntax.
Happy Hibernate querying!
---
External links:
[1] http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/Query.html
[2] http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/SharedSessionContract.html
#createQuery(java.lang.String)
[3] http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/Session.html#createFilter
(java.lang.Object, java.lang.String)
[4] http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/SharedSessionContract.html
#getNamedQuery(java.lang.String)
[5] http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/Criteria.html
[6] http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/SharedSessionContract.html
#createCriteria(java.lang.Class)
[7] http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/criterion/DetachedCriteria.html
[8] http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/criterion/DetachedCriteria.html
#getExecutableCriteria(org.hibernate.Session)
[9] http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/SharedSessionContract.html
#createSQLQuery(java.lang.String)
No comments:
Post a Comment