Magazine
 
Hibernate Query Language

Download Program

4. Querying with Projections:

Projections are used to customize the results from the database. In general Projection means to retrieve; while in case of SQL Projection means “Select” clause. The above code retrieves all the rows from the ‘insurance’ table.
But what if only the data contained in one of the fields has to be retrieved, as in the following SQL query:

SELECT NAME FROM PRODUCT

Here, the Projection class comes into play. The above query can be rewritten into a Criteria query as:

ProjectionList proList =
Projections.projectionList();
proList.add(Projections.property(“name”));
crit.setProjection(proList);

In the above code, ProjectionList is the list of projection instances, which are result of Query’s object. The fieldname is passed as an argument to the property() method of the Projection class. The Projection instance returned in turn becomes an argument to the setProjection() method.

Now lets see an example of hibernate projection:

In the class projectionExample.java, first a session object created with the help of the SessionFactory interface. Then use the createQuery() method of the Session object which returns a Query object. Now we use the openSession() method of the SessionFactory interface simply to instantiate the Session object.

 

Then the criteria object is obtained simply by invoking the createCriteria() method of the Session’s object. Now create a projectionList object add the fields having properties “name” and “price”. Set it to the Criteria object by invoking the setProjection() method and passing the projectList object into this method and then add this object into the List interface’s list object and iterate this object list object to display the data contained in this object.

Here is the full source code of projectionExample.java:

package javajzzup.hibernate;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
public class projectionExample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess = null;
try{
SessionFactory sfact = new
Configuration().configure().buildSessionFactory();
sess = sfact.openSession();
Criteria crit =
sess.createCriteria(Product.class);
ProjectionList proList =
Projections.projectionList();
proList.add(Projections.property(“name”));
proList.add(Projections.property(“price”));
crit.setProjection(proList);
List list = crit.list();
Iterator it = list.iterator();
if(!it.hasNext()){
System.out.println(“No any data!”);

 
Mar 2008 | Java Jazz Up | 49
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59,

Download PDF