Hibernate WHERE Clause

In this tutorial you will learn how to use HQL where clause.

Hibernate WHERE Clause

Hibernate WHERE Clause

In this tutorial you will learn how to use HQL where clause.

Use of WHERE clause in hibernate is as same as it used in SQL for extracting only those records from the database which fulfils the specified criteria

Example :

Here I am giving a simple example which will demonstrate you how a HQL where clause can be used. To achieve the solution of such problem at first I have created a table named employee with the field 'Id' and 'Name'. Next created a POJO / Persistent class named Employee to fetch the persistent object, a hibernate.cfg.xml file that the Hibernate can utilize it to create connection pool and the required environment setup, an employee.hbm.xml file to map an Employee object with table employee. And finally create a simple java file into which we will use HQL where clause for extracting the specified data from the database.

Complete Code :

Employee.java

package roseindia;

public class Employee {
private int id;
private String name;

public Employee()
{

}
public Employee(String name)
{
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

employee.hbm.xml

<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="roseindia">
<class name="Employee" table="employee">
<id name="id" type="int" column="Id" >
<generator class="assigned"/>
</id>
<property name="name">
<column name="Name" />
</property>
</class>
</hibernate-mapping>

HibernateWhereClause.java

package roseindia;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateWhereClause {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static void main(String args[]) {
Session session = null;
try {
try {
Configuration cfg = new Configuration().addResource(
"roseindia/employee.hbm.xml").configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);

} catch (Throwable th) {
System.err.println("Failed to create sessionFactory object."
+ th);
throw new ExceptionInInitializerError(th);
}
session = sessionFactory.openSession();
Query whereClause = session
.createQuery("from Employee as emp where emp.id = 1");
List<Employee> empList = whereClause.list();
for (Employee emp : empList) {
System.out.println("Id : " + emp.getId());
System.out.println("Name : " + emp.getName());
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.close();
}
}

}

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.10.13:3306/data
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>

</hibernate-configuration>

Output :

1. employee table

2. When you will execute the java file HibernateWhereClause.java (RightClick -> runAs -> Java Application) you will get the output as :

Download Source Code