| |
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 :
Interview Questions
Description :
On Hibernate More...
|
Written By : Amit
Title :
mapping class hierarchy table per subclass
Description :
Example More...
|
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...
|
| Tags/Keywords : Hibernate Filter, Hibernate, Filter, Example, Code, Tutorial, Article Author : ISHTEK Date (Year/Month/Date): 2009-06-21
Example on Hibernate Filter with sample code | |
Please be informed that NONE of the design/code from this
page is claiming to be some sort of best practices and we DO NOT expect
any of our visitor/reader of this page to assume this as some sort of
best practice for any context and should not be using this
as it is without appropriate evaluation to their, so to say,
specific programming context.
This page intends only to provide bit and piece of known ways for
doing some sort of example and may not be fit for any other purpose.
Applying Filter programmatically using HQL from Hibernate Framework.
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%" ).
 | 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).
|
| Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Hibernate Tutorial >>> Hibernate Filter >>> Hibernate Filter Example |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
 |
Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example
code, tips, tutorials etc. Like any printed materials, 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.
By accessing and using this website in any ways, including, without
limitation, browsing the website pages, using any information, using any content and/or
downloading any materials, you agree to and are bound by the terms of use
described in this page and Usage Terms and Conditions.
If you do not agree to all of
the terms and conditions contained in the terms of use described in this
page and Usage Terms and Conditions, do not use this
website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use
on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being
taken for providing accurate and up-to-date information but like any printed materials,
these blog(s)/contents may eventually be outdated one day, so if you are using any
of these information, please refer original content/documentation from respective sources.
And under no circumstances shall the Author of these contents and/or this web site
be liable for any loss, damage, expense incurred or suffered which is claimed to have
occurred because of usage of the contents of this web site.
If you have any questions/queries/feedback/suggestions then please write to this web
site owner at contact.
|  |
|
|
|
|
|