September 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Criteria Queries in JPA 2.0 and Hibernate: The APIs Have It

  • July 19, 2010
  • By Sangeetha S, Nitin KL
  • Send Email »
  • More Articles »

Criteria Queries in Hibernate

In Hibernate, an org.hibernate.Criteria object represents a criteria query object, which is created using a Session object. The simple example below creates a criteria object for the Employee class and returns all the instances of Employee:

Criteria queryObj = session.createCriteria(Employee.class);List<Employee> empList = queryObj.list();

The HQL for the corresponding criteria query is shown below. This is generally generated by the Hibernate framework with some configuration settings in the XML file.

HQL: Hibernate: select this_.id as id1_0_, this_.fname as fname1_0_, this_.lname as lname1_0_ from employee this_

The query results can be narrowed down by applying certain restrictions as shown below:

List<Employee> emps=session.createCriteria(Employee.class).add (Restrictions.like("Lname", "s%")).add (Restrictions.between("id", 1, 2)).list();   HQL: select this_.id as id1_0_, this_.fname as fname1_0_, this_.lname as lname1_0_ from employee this_ where this_.lname like ? and this_.id between ? and ?

Every individual query criterion applied for a criteria query is a type of org.hibernate.criterion.Criterion and a factory of built-in criterion types is defined by the org.hibernate.criterion.Restrictions class. Logical operators can also be attached to criteria queries using the Restriction class like this:

List<Employee> employeeList = session.createCriteria(Employee.class)   .add (Restrictions.like("Lname", "K%"))   .add (Restrictions.and(Restrictions.eq("Id", new Integer(24)),Restrictions.isNotNull("Id"))).list();HQL: select this_.id as id1_0_, this_.fname as fname1_0_, this_.lname as lname1_0_ from employee this_ where this_.lname like ? and (this_.id=? and this_.id is not null)

The org.hibernate.criterion.Order class can be used to order the query results as shown here:

List<Employee> employeeList=  session.createCriteria(Employee.class)         .addOrder(Order.asc("Lname"))         .addOrder(Order.desc("Id")).setMaResults(50).list(); HQL: select this_.id as id1_0_, this_.fname as fname1_0_, this_.lname as lname1_0_ from employee this_ order by this_.lname asc, this_.id desc limit ?

Query by Example, a powerful feature introduced in Hibernate 3.0, allows a criteria query to be supplied with an example instance, which is set with properties that need to be retrieved. Here is an example:

Employee exampleObject = new Employee();exampleObject.setId(3);exampleObject.setFname("Nischith");exampleObject.setLname("K L");   List<Employee> resultEmp = session.createCriteria(Employee.class).add(Example.create(exampleObject)).list();HQL: select this_.id as id1_0_, this_.fname as fname1_0_, this_.lname as lname1_0_ from employee this_ where (this_.fname=? and this_.lname=?)

Conclusion

In this article we compared the features for criteria queries in JPA 2 with those available in Hibernate. In particular, we discussed the introduction of the Metamodel APIs for criteria queries in JPA 2, a differentiating feature from Hibernate.

Acknowledgements

The authors would like to sincerely thank Mr. Subrahmanya SV (VP, ECOM Research Group, E&R) for his ideas, guidance, support and constant encouragement and Ms. Mahalakshmi for kindly reviewing this article and providing valuable comments.

About the Authors

Sangeetha S. works as a Senior Technical Architect at the E-Commerce Research Labs at Infosys Technologies. She has over 10 years of experience in design and development of Java and Java EE applications. She has co-authored a book on 'J2EE Architecture' and also has written articles for online Java publications.

Nitin KL works at the E-Commerce Research Labs at Infosys Technologies. He is involved in design and development of Java EE applications using Hibernate, iBATIS, and JPA.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel