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 :
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...
|
Written By : Amit
Title :
Many to Many Mapping Example
Description :
code More...
|
Written By : Amit
Title :
Interview questions answer
Description :
discussion More...
|
| one to many Hibernate, Mapping, one, Many, Hibernate, Tutorial, Article Author : Amit Date (Year/Month/Date): 2009-02-18
One to Many mapping and writing HBM file | |
Hibernate mapping explained:
One-to-Many type of mapping
example:
Case study: A Department can
have many employees and an
Employee can be assigned to
one Department.
So Employee and Department are
having one to many type of
mapping, is explained here with
a one-to-many type of mapping
in Hibernate.
|
|
So Department and Employees can share a one-to-many type of mapping.
My Software environment for this example:
1. Eclipse 3.2
2. JDK 1.5
3. Hibernate 3.2
Hibernate one to many mapping can be realized by using specific tags
like <one-to-many>, but I am using it along with <set> tag.
In this example, POJO (Plain Old Java Object) are Dept and Employee.
/**
* 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
*/
Dept.java
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;
}
}
Dept is a simple POJO class with a employees field declared
as Set for storing all employees in it.
/**
* 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
*/
Employee.java
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;
}
}
HBM mapping file is as mentioned below:
In order to create this mapping, first of all I placed normal <class>
tag for Dept and Employee, with a id for the primary key and property tags
for each fields within Dept and Employee.
In order to use <one-to-many> tag , one should have a collection
of many items at one side, and we can see employees field in Dept.
So there has to have a collection tag like <set> tag inside <class>
tag for Dept.
<?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>
Corresponding tables in database (for this example, I have used HSQLDB), following
are the SQL (for HSQLDB as database):
create table dept
(dept_id integer, dept_name varchar(20), primary key (dept_id))
create table employee
(employee_id integer, employee_name varchar(50), dept_id integer,
primary key (employee_id), foreign key(dept_id) references dept(dept_id));
hibernate.cfg.xml file that contains all the configuration settings for this
example is as shown below:
<?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>
Client code for testing this example could be something like as follows:
If wanted to have complete source code of this Client program, then
Please REFER THIS
if(sessionFactory != null) {
Session session = sessionFactory.getCurrentSession();
Transaction trans = session.getTransaction();
trans.begin();
Employee emp1 = new Employee();
emp1.setEmployeeId(1001);
emp1.setEmployeeName("test1");
Employee emp2 = new Employee();
emp2.setEmployeeId(1002);
emp2.setEmployeeName("test2");
Employee emp3 = new Employee();
emp3.setEmployeeId(1003);
emp3.setEmployeeName("test3");
Set emps = new HashSet();
emps.add(emp1);
emps.add(emp2);
emps.add(emp3);
Dept dept = new Dept();
dept.setDeptId(1001);
dept.setDeptName("test dept1");
dept.setEmployees(emps);
session.persist(dept);
trans.commit();
}
Here in this client, I am just creating three employees and then
creating a dept and associating these already created employees to dept.
This way, I am getting a following console as output:
Hibernate: insert into dept (dept_name, dept_id) values (?, ?)
Hibernate: insert into employee (employee_name, employee_id) values (?, ?)
Hibernate: insert into employee (employee_name, employee_id) values (?, ?)
Hibernate: insert into employee (employee_name, employee_id) values (?, ?)
Hibernate: update employee set dept_id=? where employee_id=?
Hibernate: update employee set dept_id=? where employee_id=?
Hibernate: update employee set dept_id=? where employee_id=?
This means I have managed to create a one to many mapping persisted in
database tables.
 | 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).
|
|