Advertisement
Home > Hibernate Tutorial > Hibernate Filter > Hibernate Filter ExamplePlease log in to add or reply to any matter<- requires login or
RMI Example

Home > Hibernate Tutorial > Hibernate Filter > Hibernate Filter Example
Struts Tutorials:
Struts2 Tag CheckBoxList
, Checkbox, Iterator, IF
Struts2 Tag Library Example Struts2 Tiles Example Struts2 Tiles I18N Example Struts2 Questions Struts Tiles I18N Example Struts Eclipse MVC Struts2 Tags Struts2 Example and Tutorial Struts MVC Struts2 Validation
Hibernate Tutorials: Hibernate Case Study Class Hierarchy Persist Example Using Hibernate Interceptor Hibernate Questions with Answer Hibernate Many-to-Many Mapping Example Hibernate one-to-many Mapping Example Hibernate and ORM tools Spring Hibernate Example Hibernate SessionFactory Example Hibernate Mapping Class Hierarchy Hibernate Questions Hibernate SessionFactory Questions Spring Hibernate Example: Spring Hibernate Case Study

Written By : Amit
Title :
ways create Hibernate SessionFactory
Description : Example
More...


Written By : Amit
Title :
Spring Hibernate Integration example
Description : Example
More...


Written By : Amit
Title :
ORM Hibernate Best FIT
Description : Discussion
More...


Written By : Amit
Title :
One to Many mapping example
Description : Code
More...


Written By : Amit
Title :
Many to Many Mapping Example
Description : code
More...


Written By : Amit
Title :
Interview questions answer
Description : discussion
More...


Written By : Amit
Title :
Interceptor use log SQL statements
Description : example code
More...


Written By : Amit
Title :
Class Hierarchy Persist example
Description : code
More...

Hibernate Filter, Hibernate, Filter, Example, Code, Tutorial, Article
Author : ISHTEK
Date (Year/Month/Date): 2009-06-21 Example on Hibernate Filter with sample code
Applying Filter programmatically using HQL from Hibernate Framework.

Advertisement
In order to explain Hibernate Filter, I would use the same example of Employee and Department, as already used in many such writings of mine, in this site. In this example of Hibernate Filter, we have two entities, such as Employee and Dept, with two database tables namely, employee and dept. Following example has the software environment as follows: 1. JDK 5.0 (Java Platform) 2. Eclipse 3.2.0 (IDE) 3. Hibernate 3.2 (Hibernate Framework) 4. HSQLDB 1.7.3 (Database) Dept and Employee entities share one-to-many type of Hibernate Mapping, and the HBM mapping files details as follows: (One can use this for personal and learning purpose only, no commercial use allowed)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="demo">
  <class name="Dept" table="dept">
    <id name="deptId" column="dept_id">
      <generator class="assigned"/>
    </id>
    <property name="deptName" column="dept_name" type="java.lang.String"/>
    <set name="employees" table="employee" cascade="persist,delete">
      <key column="dept_id" />
      <one-to-many class="Employee"/>
    </set>
  </class>
  <class name="Employee" table="employee">
    <id name="employeeId" column="employee_id">
      <generator class="assigned"/>
    </id>
    <property name="employeeName" column="employee_name" 
                                     type="java.lang.String"/>
    <one-to-one name="dept" class="Dept" />
  </class>
</hibernate-mapping>
Looking at the collection of employees configured as SET with name employees in the HBM file, shows that Dept entity has a variable employees with setter and getter methods for holding corresponding Employees records from database. HSQLDB database related SQL script for creation of these two tables, as follows
create table dept
(dept_id integer, dept_name varchar(100), primary key (dept_id));

create table employee
(employee_id integer, employee_name varchar(100), dept_id integer,
 primary key (employee_id), foreign key (dept_id) references dept(dept_id));

insert into dept
values('1001', 'example Department');

insert into employee
values('2001','employee name 1','1001');

insert into employee
values('2002','employee name 2','1001');

insert into employee
values('2003','employee name 3','1001');
Hibernate Entities for this example, includes two POJO, such as Employee and Dept, as shown below: Employee.java /** * This source is provided as is, without any warranty * and /or guaranty of any kind. * Copyright (C) 2008, ISHTIAK, All Rights Reserved. * You can use it for Personal Learning purpose only. * E-mail: usingframeworks@gmail.com */ package demo; public class Employee { private int employeeId; private String employeeName; private Dept dept; public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } 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; } } Dept.java /** * This source is provided as is, without any warranty * and /or guaranty of any kind. * Copyright (C) 2008, ISHTIAK, All Rights Reserved. * You can use it for Personal Learning purpose only. * E-mail: usingframeworks@gmail.com */ package demo; import java.util.Set; public class Dept { private int deptId; private String deptName; private Set employees; public int getDeptId() { return deptId; } public void setDeptId(int deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public Set getEmployees() { return employees; } public void setEmployees(Set employees) { this.employees = employees; } } Hibernate configuration file for this Hibernate Filter example is as follows: This file will used by this example Client program to create Hibernate SessionFactory instance: <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- properties --> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:hsql://localhost/</property> <property name="connection.username">sa</property> <property name="connection.password"></property> <property name="dialect">org.hibernate.dialect.HSQLDialect</property> <property name="show_sql">true</property> <property name="current_session_context_class">thread</property> <!-- mapping files --> <mapping resource="demo/DeptsEmployee.hbm.xml"/> </session-factory> </hibernate-configuration> As you can see, I have used HSQLDB database server, and if anyone not used this database server before, just for information, you can start this HSQLDB database server by using a script file at %HSQLDB%\demo\runServer.bat . This script file will start HSQLDB database server. In order to run all the SQL (create table, and insert SQL scripts) from this example, you have to run the HSQLDB manager from %HSQLDB%\demo\runManager.bat script file. From the HSQLDB manager GUI confihuration dialog box, you have to choose "HSQL Database Engine Server" from the drop down menu for "TYPE". After the default HSQLDB database setup for this example is done, once can write the test client for this Hibernate Filter Example. In the test client, objective is to test the Filter criteria being set programmatically and based on the filter criteria, one should be able to get appropriate list of employee records/Entities. Client.java /** * This source is provided as is, without any warranty * and /or guaranty of any kind. * Copyright (C) 2008, ISHTIAK, All Rights Reserved. * You can use it for Personal Learning purpose only. * E-mail: usingframeworks@gmail.com */ package demo; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Restrictions; public class Client { // Hibernate SessionFactory SessionFactory sessionFactory; public Client() { //Hibernate Configuration Configuration conf = new Configuration(); //Loading configuration properties file and building Hibernate SessionFactory. sessionFactory = conf.configure("hibernate.cfg.xml") .buildSessionFactory(); if(sessionFactory != null) { Session session = sessionFactory.getCurrentSession(); Transaction trans = session.getTransaction(); trans.begin(); Dept dept = (Dept)session.get(Dept.class, new Integer(1001)); Query query = session.createFilter(dept.getEmployees(), "where employeeName like :employeeName1"); query.setParameter("employeeName1", "%employee name%"); List list = query.list(); System.out.println("Number of employee records fetched from database : " +list.size()); trans.commit(); } } /** * Client main method * @param args */ public static void main(String[] args) { new Client(); } } This test client will show the output as Number of employee records fetched from database : 3 As there are total three Employee records exists for the criteria as where employeeName like '%employee name%' createFilter method of Hibernate Session instance will accept a persistent entity (in this case it is Dept instance retrieved from database for dept_id as 1001), and the query string (HQL criteria, such as where employeeName like :employeeName1, employeeName1 being the parameter with value set as "%employee name%" ).
Advertisement
Author of this article/writeup has expressed his/her willingness
to help or guide users with any technical difficulties he/she faces while working with the example code environment setting up, running and resolving any such exception raised during compile or at runtime. You may ask for any technical doubt or seek technical help related to this article by using following form to reach for technical help from the Author for FREE. This article's Author shall be reading your request and responding within reasonable time (no resolution timeframe defined as such).
Please write your Comment on this Matter
(This will be visible if found suitable):
Name: *
Email (will not be displayed): *
Matter: *
40,15
Enter bigger number from above :*
Home > Hibernate Tutorial > Hibernate Filter > Hibernate Filter Example
Visitor/User submitted related resources:
(Visible upon review and approved by this site Administrator)
Referred By Name *:
Resource URL *: (e.g, URL should be starting with http://www.-----.---)
 
Resource Short Description *:
23,12
Enter bigger number from above : *

Please log in to add or reply to any matter<- requires login
Log in or Register
This List is generated as on 2009-07-12 (YYYY-MM-DD)
#Discuss-these : questions-and-answer : Interview-Questions-on-Java
#Question-on-Session-Factory-Creation : SessionFactory : Hibernate-Tutorial
#On-Hibernate : Interview-Questions : Hibernate-Tutorial
#Example : mapping-class-hierarchy-table-per-subclass : Hibernate-Tutorial
#Example : ways-create-Hibernate-SessionFactory : Hibernate-Tutorial
#Example : Spring-Hibernate-Integration-example : Hibernate-Tutorial
#Discussion : ORM-Hibernate-Best-FIT : Hibernate-Tutorial
#Code : One-to-Many-mapping-example : Hibernate-Tutorial
#code : Many-to-Many-Mapping-Example : Hibernate-Tutorial
#discussion : Interview-questions-answer : Hibernate-Tutorial
#example-code : Interceptor-use-log-SQL-statements : Hibernate-Tutorial
#code : Class-Hierarchy-Persist-example : Hibernate-Tutorial
#discussion : Case-study-Hibernate : Hibernate-Tutorial
#With-Answer : Hibernate-Interview-Questions : Hibernate-Tutorial
#Discussed : Hibernate-Interview-questions-answer : Hibernate-Tutorial
#Question : Hibernate-Transaction : Hibernate-Tutorial
#Example-Case-study : Contextual-Session : Hibernate-Tutorial
#Design-with-example-code : Struts2-Hibernate : Hibernate-Tutorial
#Hibernate-query : Hibernate-Criteria : Hibernate-Tutorial
#Hibernate-Criteria-Example : Hibernate-Filter-Example : Hibernate-Tutorial
#Hibernate-Filter-Example : Hibernate-Filter : Hibernate-Tutorial
#Hibernate-Sample : Hibernate-many-to-one : Hibernate-Tutorial
#Hibernate-Sample-Example : Hibernate-named-query : Hibernate-Tutorial
#Composite-Primary-Key : Hibernate-Composite-Key : Hibernate-Tutorial
#Mapping-Example : Hibernate-one-to-one : Hibernate-Tutorial
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved.
CONTACT    PRIVACY POLICY    DISCLAIMER

This web site provides some of the information about various technologies, example 
code, tips, tutorials etc. Like any printed matterials, content of these pages may 
become out of date over a period of time. Therefore all visitor/users of this web 
site are requested/advised to refer to the originating parties/sources for the 
latest changes and happenings for detailed information. This information is not 
intended to be a substitute for the original reference provided by the originating 
parties/sources.