HQL Between clause, HQL Between example
Posted on: November 2, 2010 at 12:00 AM
See the example of HQL Between clause.

HQL Between clause example

In this tutorial you will learn how to use HQL Between clause to select a range of entity falling between 2 and 6. You can change the values to get entities as per your requirement.

In this example code we are selecting all the customer object having primary key (id) between 2 and 6.

The HQL Between used for this purpose is:

// Create HQL Between clause
String HQL_QUERY = "select c from Customer c where c.id between 2 and 6";

To run the example you should have some records in the Customer table. Following SQL can be used to create table in database.


CREATE TABLE `customers` (
  `customer_name` varchar(50) DEFAULT NULL,
  `customer_phone` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)

/*Data for the table `customers` */

insert  into `customers`(`id`,`customer_name`,`customer_phone`)
  values (1,'Deepak','222222222');
insert  into `customers`(`id`,`customer_name`,`customer_phone`) 
  values (2,'Ravi','6565666877878');
insert  into `customers`(`id`,`customer_name`,`customer_phone`) 
  values (3,'John','154545655');
insert  into `customers`(`id`,`customer_name`,`customer_phone`) 
  values (4,'Dermot','455666');
insert  into `customers`(`id`,`customer_name`,`customer_phone`) 
  values (5,'Raju','5454555');
insert  into `customers`(`id`,`customer_name`,`customer_phone`) 
  values (6,'Dinesh','455455454');

Here is the code of Entity class:

package net.roseindia.model;
import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;

 * @author Deepak Kumar
 * Hibernate HQL Tutorials
public class Customer {
	@Column(name = "id")  
	private Integer id;
	private String customerName;
	private String customerPhone;
	public Integer getId() {
		return id;
	public void setId(Integer id) {
		this.id = id;
	public String getCustomerName() {
		return customerName;
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	public String getCustomerPhone() {
		return customerPhone;
	public void setCustomerPhone(String customerPhone) {
		this.customerPhone = customerPhone;

Code of HQLBetween.java file:

package net.roseindia.hqlexamples;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import net.roseindia.model.*;

 * HQL Select Example In this example we are selecting all the customers from
 * the datastore

public class HQLBetween {

	public static void main(String[] args) throws Exception {
		/** Getting the Session Factory and session */
		SessionFactory factory = HibernateUtil.getSessionFactory();
		Session session = factory.getCurrentSession();
		/** Starting the Transaction */
		Transaction tx = session.beginTransaction();

		// Create HQL Between clause
		String HQL_QUERY = "select c from Customer c where c.id between 2 and 6";
		Query query = session.createQuery(HQL_QUERY);
		List result = query.list();
		for (Iterator it = result.iterator(); it.hasNext();) {
			Customer customer = (Customer) it.next();
			System.out.println("ID: " + customer.getId());
			System.out.println("Name: " + customer.getCustomerName());
			System.out.println("Phone: " + customer.getCustomerPhone());

		/** Closing Session */

You can run the HQL between example code by executing the HQLBetween  class. Following is the output of the code:

Hibernate: select customer0_.id as id0_, customer0_.customer_name as customer2_0_, customer0_.customer_phone as customer3_0_ from customers customer0_ where customer0_.id between 2 and 6

ID: 2

Name: Ravi

Phone: 6565666877878

ID: 3

Name: John

Phone: 154545655

ID: 4

Name: Dermot

Phone: 455666

ID: 5

Name: Raju

Phone: 5454555

ID: 6

Name: Dinesh

Phone: 455455454

Download HQL Select Source Code

Related Tags for HQL Between clause, HQL Between example:




Advertisement null