In the previous tutorial, we have discussed about Hibernate Filters using Xml Configuration. The same example we are going to implement here with Annotation configuration.

Here is the complete example for Hibernate Filters :

Technologies :

  • Hibernate 4.3
  • Java7

Hibernate Filters Example :

Project Structure :

Hibernate Filters Example-min

Required Dependencies :



Create Employee Pojo :


package com.otp.hibernate.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
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 = "employee")
@FilterDef(name = "employeeFilter", parameters = @ParamDef(name = "salary", type = "int"))
@Filter(name = "employeeFilter", condition = "salary >:salary")
public class Employee {
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    private int employeeId;
    @Column(name = "ename")
    private String employeeName;
    @Column(name = "salary")
    private int salary;
    @Column(name = "deptNo")
    private int deptNo;

    public int getEmployeeId() {
        return employeeId;

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;

    public String getEmployeeName() {
        return employeeName;

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;

    public int getSalary() {
        return salary;

    public void setSalary(int salary) {
        this.salary = salary;

    public int getDeptNo() {
        return deptNo;

    public void setDeptNo(int deptNo) {
        this.deptNo = deptNo;



On the above Annotations :

  • @FilterDef annotation is an exact replacement of <filter-def> tag in xml.
  • As we discussed in the previous tutorial, we can pass the parameters to FilterDef.
  • By using the parameters attribute, we can pass the parameters to FilterDef.

Example :

parameters = @ParamDef(name = "salary", type = "int")
  • @Filter annotation is an exact replacement of <filter> tag in xml.
  • We can pass the conditions to Filter.
  • By using the condition attribute we can do this like below.
  • condition = “salary >:salary”

Run It :


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

import org.hibernate.Filter;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.stat.Statistics;

import com.otp.hibernate.pojo.Employee;

public class Main {

    public static void main(String[] args) {
        Configuration configuration = new Configuration()
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
        SessionFactory factory = configuration.buildSessionFactory(builder
        Session session = factory.openSession();
        Filter filter = session.enableFilter("employeeFilter");
        filter.setParameter("salary", 4000);
        Query query = session.createQuery("from Employee e");
        List list = query.list();
        Iterator it =list.iterator();
        while (it.hasNext()) {
            Employee emp = (Employee);
            System.out.println("Employee Name : "+emp.getEmployeeName() +" , Salary : "+emp.getSalary());



Before going to run the application, I am confirming that the data in employee table like below.

Hibernate Filters Example Database-min

Based on the filter configuration and condition, we can get the employee names who’s salaries are greater than 4000.

Lets Check 🙂

Output :

Employee Name : Chandra , Salary : 6000
Employee Name : Shekhar , Salary : 8000
Employee Name : Mahesh , Salary : 5000

Happy Learning 🙂

Download Example