Advertisement
Home > Hibernate Tutorial > Hibernate many to one > Hibernate SamplePlease log in to add or reply to any matter<- requires login or
RMI Example

Home > Hibernate Tutorial > Hibernate many to one > Hibernate Sample
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...

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

Understanding left outer join using Hibernate Mapping and fetching strategy

Following example has the software environment as follows:

Advertisement
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_idemployee_nameskillset_iddept_id
3001girish20021002
3002steve20021003
3003crish20011001
3004suresh  
3005guddu  
3006Tom  
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_iddept_name
1001HR
1002Finance
1003Stores
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
2001Java
2002J2EE
2003Struts
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_nameskillset_descdept_name
girishJ2EEFinance
steveJ2EEStores
crishJavaHR
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
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: *
16,13
Enter bigger number from above :*
Home > Hibernate Tutorial > Hibernate many to one > Hibernate Sample
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 *:
11,6
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.