| |
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...
|
| Tags/Keywords : Hibernate one to many, Hibernate many to one, Hibernate sample, Join, Hibernate Join, Fetch Author : ISHTEK Date (Year/Month/Date): 2009-06-24
Understanding left outer join using Hibernate Mapping and fetching strategy | |
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.
Understanding left outer join using Hibernate Mapping and fetching strategy
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)
Let us take an example case study :
There are three Hibernate Entities such as Employee, Department and Skillset.
A department can have many employees and each employee can have zero or more
skill set. So Employee Persistent Entity is mapped to Department persistent
Entity as many-to-one mapping strategy. Department and Skillset persistent
Entities are mapped to Employee as one-to-many Hibernate mapping strategy.
Domain model as follows:
This is represented in form of the following HBM file (of this example):
<?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">
<key column="dept_id"/>
<one-to-many class="Employee"/>
</set>
</class>
<class name="Skillset" table="Skillset">
<id name="skillsetId" column="skillset_id">
<generator class="assigned"/>
</id>
<property name="skillSetDesc" column="skillset_desc"
type="java.lang.String"/>
<set name="employees">
<key column="skillset_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"/>
<many-to-one name="dept" column="dept_id" class="Dept" fetch="join"/>
<many-to-one name="skillSet" column="skillset_id" class="Skillset"
fetch="join"/>
</class>
</hibernate-mapping>
|
Objective of this example is to explain OUTER JOIN capability of Hibernate
by setting fetch as "join" in many-to-one type of Hibernate Mapping.
So let us comeup with sample test data for validating this objective
against SQL query result.
Structure of Employee Table with test data are as follows:
All DML in this example are for HSQLDB database only
create table Employee
(employee_id integer, employee_name varchar(100), skillset_id integer, dept_id integer,
primary key(employee_id),
foreign key(skillset_id) references skillset(skillset_id),
foreign key (dept_id) references Dept(dept_id));
|
| employee_id | employee_name | skillset_id | dept_id |
| 3001 | girish | 2002 | 1002 |
| 3002 | steve | 2002 | 1003 |
| 3003 | crish | 2001 | 1001 |
| 3004 | suresh | | |
| 3005 | guddu | | |
| 3006 | Tom | | |
Structure of Dept table along with test data as follows:
create table Dept
(dept_id integer, dept_name varchar(50), primary key(dept_id));
|
| dept_id | dept_name |
| 1001 | HR |
| 1002 | Finance |
| 1003 | Stores |
Structure of Skill Set table along with test data as follows:
create table Skillset
(skillset_id integer, skillset_desc varchar(50), primary key(skillset_id));
|
| skillset_id | skillset_desc |
| 2001 | Java |
| 2002 | J2EE |
| 2003 | Struts |
Corresponding INSERT statements are as follows:
insert into Dept values(1001, 'HR');
insert into Dept values(1002, 'Finance');
insert into Dept values(1003, 'Stores');
insert into skillset values(2001, 'Java');
insert into skillset values(2002, 'J2EE');
insert into skillset values(2003, 'Struts');
insert into Employee values(3001,'girish',2002,1002);
insert into Employee values(3002,'steve',2002,1003);
insert into Employee values(3003,'crish',2001,1001);
insert into Employee(employee_id,employee_name) values(3004,'suresh');
insert into Employee(employee_id,employee_name) values(3005,'guddu');
insert into Employee(employee_id,employee_name) values(3006,'Tom');
|
Suppose one has to write a SQL query to create a report whereby
all employee names along with their corresponding skill set and
department are to be shown, then the SQL statement will be
something like as follows:
select e.employee_name, s.skillset_desc, d.dept_name from Employee e
left outer join Dept d on e.dept_id = d.dept_id left outer join
Skillset s on s.skillset_id = e.skillset_id
|
And the result will be as shown below:
| employee_name | skillset_desc | dept_name |
| girish | J2EE | Finance |
| steve | J2EE | Stores |
| crish | Java | HR |
| suresh | | |
| guddu | | |
| Tom | | |
This example will try to create appropriate mapping using Hibernate
for fetching similar set of data, using fetch="join" in
many-to-one tag used with Employee persistent Entity.
Test client for this example is very simple, it is just creating
Hibernate criteria by using Hibernate Session instance, and
the code snippet is as follows:
List list = session.createCriteria(Employee.class).list();
Reference:
Remaining Entities for this example are as follows:
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;
private Skillset skillSet;
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;
}
public Skillset getSkillSet() {
return skillSet;
}
public void setSkillSet(Skillset skillSet) {
this.skillSet = skillSet;
}
}
|
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;
}
}
|
Skillset.java
package demo;
import java.util.Set;
public class Skillset {
private int skillsetId;
private String skillSetDesc;
private Set employees;
public Set getEmployees() {
return employees;
}
public void setEmployees(Set employees) {
this.employees = employees;
}
public String getSkillSetDesc() {
return skillSetDesc;
}
public void setSkillSetDesc(String skillSetDesc) {
this.skillSetDesc = skillSetDesc;
}
public int getSkillsetId() {
return skillsetId;
}
public void setSkillsetId(int skillsetId) {
this.skillsetId = skillsetId;
}
}
|
After running thie example client following is the SQL (auto generated by Hibernate)
shown in my system console:
select this_.employee_id as employee1_2_2_, this_.employee_name as
employee2_2_2_, this_.dept_id as dept3_2_2_, this_.skillset_id as
skillset4_2_2_, dept2_.dept_id as dept1_0_0_, dept2_.dept_name as dept2_0_0_,
skillset3_.skillset_id as skillset1_1_1_, skillset3_.skillset_desc as
skillset2_1_1_ from employee this_ left outer join Dept dept2_ on
this_.dept_id=dept2_.dept_id left outer join Skillset skillset3_ on
this_.skillset_id=skillset3_.skillset_id
 | 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 many to one >>> Hibernate Sample |
|
|
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.
|  |
|
|
|
|
|