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 :
Required Dependencies :
pom.xml
[xml]
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
[/xml]
Create Employee Pojo :
Employee.java
[java]
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;
@Entity
@Table(name = "employee")
@FilterDef(name = "employeeFilter", parameters = @ParamDef(name = "salary", type = "int"))
@Filter(name = "employeeFilter", condition = "salary >:salary")
public class Employee {
@Id
@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;
}
}
[/java]
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 :
Main.java
[java]
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()
.configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
SessionFactory factory = configuration.buildSessionFactory(builder
.build());
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) it.next();
System.out.println("Employee Name : "+emp.getEmployeeName() +" , Salary : "+emp.getSalary());
}
}
}
[/java]
Before going to run the application, I am confirming that the data in employee table like below.
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 🙂