Saturday , April 29 2017
Home / Hibernate / Hibernate Filters Example Annotation

Hibernate Filters Example Annotation

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

Here is the complete example for Hibernate Filters :

Hibernate Filters Example :

Create a project structure like below.

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 the 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 the 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”

Test the Application :

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 assuming 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

Download Example

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Check Also

Hibernate Projection with Example

In this tutorials, we are going to learn about Hibernate Projection with a simple example. …

One comment



    Do you know some way to make Hibernate Filters work with JPA?


Leave a Reply

Your email address will not be published. Required fields are marked *