Hibernate Filters Annotation tutorial and example

Hibernate filters

Hibernate3 provides an innovative new approach to handling data with "visibility" rules. A Hibernate filter is a global, named, parameterized filter that can be enabled or disabled for a particular Hibernate session.

Hibernate3 has the ability to pre-define filter criteria and attach those filters at both a class level and a collection level.

A filter criteria allows you to define a restriction clause similar to the existing "where" attribute available on the class and various collection elements.

These filter conditions, however, can be parameterized. The application can then decide at runtime whether certain filters should be enabled and what their parameter values should be.

Filters can be used like database views, but they are parameterized inside the application.

The @FilterDef annotation is used to specify a @Filter definition (name, default condition and parameter types, if any).

Defining a filter

@FilterDef(name = "statusFilter", parameters = { @ParamDef(name = "state", type = "string") })
@Filter(name = "statusFilter", condition = "std_status= :state ")

Enabling filter and passing parameter in Dao

Filter filter = session.enableFilter("statusFilter");
		filter.setParameter("status", "status");




package com.candidjava.hibernate;

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

import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;

@Table(name = "student4")
@FilterDef(name = "statusFilter", parameters = { @ParamDef(name = "state", type = "string") })
@Filter(name = "statusFilter", condition = "std_status= :state ")
public class Student {

	@Column(name = "Sno")
	private long id;
	@Column(name = "Name")
	private String name;
	@Column(name = "degree")
	private String degree;
	@Column(name = "Mobile")
	private String phone;
	@Column(name = "std_status")
	private String status;

	public long getId() {
		return id;

	public String getName() {
		return name;

	public String getDegree() {
		return degree;

	public String getPhone() {
		return phone;

	public void setId(long string) {
		id = string;

	public void setName(String string) {
		name = string;

	public void setDegree(String string) {
		degree = string;

	public void setPhone(String string) {
		phone = string;

	public String getStatus() {
		return status;

	public void setStatus(String string) {
		status = string;

	public String toString() {
		return name;

Get or fetch record using hibernate filter

public List<Student> getStudentfilter(String status){
		List<Student> list = null;
		try {
			Session session = getSession();// getting sessionImpl reference

			Filter filter = session.enableFilter("statusFilter");
			filter.setParameter("status", "status");

			list = session.createCriteria(Student.class).list();

		} catch (HibernateException e) {
		return list;



Hibernate filter annotation example war

Hibernate filter annotation example zip

Related Post