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.
<hibernate-mapping> ----- <class name="--" table="--"> ------ <filter name="--" condition="--"/> </class> <filter-def name="--"> <filter-param name="--" type="--"/> </filter-def> ---- </hibernate-mapping>
- 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; } }
employee.hbm.xml
<?xml version = "1.0" encoding = "utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <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"/> </class> <filter-def name="empFilter"> <filter-param name="type" type="string"/> </filter-def> </hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <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"/> </session-factory> </hibernate-configuration>
ClientApp.java
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(); cfg.configure(); 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.println("1.PERM"); System.out.println("2.TEMP"); System.out.print("Your Option :"); String etype = s.next(); filter.setParameter("type", etype); List<Employee> list = query.list(); System.out.println("ENO\tENAME\tESAL\tEADDR\tETYPE"); System.out.println("-----------------------------------------"); for (Employee emp : list) { System.out.println(emp.getEno() + "\t" + emp.getEname() + "\t" + emp.getEsal() + "\t" + emp.getEaddr() + "\t" + emp.getEtype()); } session.close(); sessionFactory.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (null != session) { session.close(); } if (null != sessionFactory) { sessionFactory.close(); } } } }