Hibernate Filters

Hibernate Filters

In Hibernate Applications, by using HQL queries, we are able to retrieve multiple records of data from database table. While retrieving multiple Records from Database table if we want to filter the results on the basis of a particular condition then we have to use “Hibernate Filters”.

If we want to use Filters in Hibernate applications we have to use the following steps.

1. Prepare a table in Database with the required columns, where we must define a filter parameter column
create table emp(ENO number primary key, ENAME varchar2(10), ESAL float, EADDR varchar2(10), ETYPE varchar2(10));

Here “ETYPE” is Filter parameter column, by using this Filter parameter column only we are able to prepare Filter condition in order to filter the results.

2. Define Filter in mapping File

To define Filter in mapping file we have to use the following XML tags in mapping file.

  <class name="--" table="--">
     <filter name="--" condition="--"/>
  <filter-def name="--">
  <filter-param name="--" type="--"/>
  • Where tag can be used to define a filter in Hibernate mapping file.
  • Where “name” attribute in tag is able to take logical name of the filter.
  • Where tag is a child tag to tag , it will define a particular filter parameter.
  • Where “name” attribute in tag will take java type like “string”, “byte”, “int”, etc.
  • Where “” tag under tag is able to configure Filter to mapping.
  • Where “name” attribute in tag is able to take logical of the filter which we have defined in ” tag.
  • Where “condition” attribute in tag is able to define condition in order to filter the results.
3. Enable Filter in Session and set values to filter parameters in order to filter the results

To enable Filter in Session we have to use the following method from org. hibernate.Session.

public Filter enableFilter(String logicalName)

To set values to the Filter parameter we will use the following method.

public void setParameter(String filter_Name, xxx value)

Where xxx may be byte, short, int, etc.

To disable Filter , we will use the following method from org.hibernate. Session.

public void disableFilter(String logicalName)
package com.ashok.hibernate.model;

 * @author ashok.mariyala
public class Employee {
	private int eno;
	private String ename;
	private float esal;
	private String eaddr;
	private String etype;

	public int getEno() {
		return eno;

	public void setEno(int eno) {
		this.eno = eno;

	public String getEname() {
		return ename;

	public void setEname(String ename) {
		this.ename = ename;

	public float getEsal() {
		return esal;

	public void setEsal(float esal) {
		this.esal = esal;

	public String getEaddr() {
		return eaddr;

	public void setEaddr(String eaddr) {
		this.eaddr = eaddr;

	public String getEtype() {
		return etype;

	public void setEtype(String etype) {
		this.etype = etype;


<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"

	<class name="com.ashok.hibernate.model.Employee" table="emp">
		<id name="eno"/>
        <property name="ename"/>
        <property name="esal"/>
        <property name="eaddr"/>
        <property name="etype"/>
        <filter name="empFilter" condition=":type = ETYPE"/>
    <filter-def name="empFilter">
       <filter-param name="type" type="string"/>


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">ashok</property>

      <mapping resource="com/ashok/hibernate/employee.hbm.xml"/>


package com.ashok.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import java.util.List;
import java.util.Scanner;

import org.hibernate.Filter;

import com.ashok.hibernate.insert.model.Employee;

 * @author ashok.mariyala
public class ClientApp {
	public static void main(String[] args) throws Exception {
		SessionFactory sessionFactory = null;
		Session session = null;
		try {
			Configuration cfg = new Configuration();
			StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
			builder = builder.applySettings(cfg.getProperties());
			StandardServiceRegistry registry = builder.build();
			sessionFactory = cfg.buildSessionFactory(registry);
			session = sessionFactory.openSession();
			Query query = session.createQuery("from Employee");
			Filter filter = session.enableFilter("empFilter");
			Scanner s = new Scanner(System.in);
			System.out.println("Employee Types:");
			System.out.print("Your Option :");
			String etype = s.next();
			filter.setParameter("type", etype);

			List<Employee> list = query.list();
			for (Employee emp : list) {
				System.out.println(emp.getEno() + "\t" + emp.getEname() + "\t" + emp.getEsal() + "\t" + emp.getEaddr()
						+ "\t" + emp.getEtype());
		} catch (Exception e) {
		} finally {
			if (null != session) {
			if (null != sessionFactory) {
Hibernate Filters
Scroll to top