One-to-Many Example Hibernate

One-to-Many Example Hibernate

One-to-Many Hibernate

Earlier in hierarchical relationships, when beans with hierarchical relationship exist, we created tables with different strategies like table-per-subclass etc. Now the other way is we will have tables with foreign key relational associations, let us create bean classes accordingly and insert records to all the tables.

The most unidirectional relational associations are

1. One-to-many
2. Many-to-one
3. One-to-one
4. many-to-many (with Set)
5. many-to-many (with Map)

Let us discuss one by one; now the first one.

  1. One-To-Many Relationship: A relationship in which each record in one table is linked (or associated) to multiple records in another table. To talk in Java style, one object of one Java class is related to multiple objects of another Java class. The two tables are associated with a foreign key. The primary key of one table is the foreign key for another table.
  2. Concept: One department may contain many employees. Here, department is table dept and employee is table employee.
  3. Association in tables: dept_id of dept table is declared as foreign key in employee table. That is, dept_id exist in both the tables.
  4. Bean coding: Let us see how to implement the above association in Java classes (beans). Two beans exist, Dept and Employee. As department contains many employees, many Employee objects are created and added to a java.util.Set. This set is added to Dept object.

Observe the variables of Dept class.

private int deptId;
private String deptName;
private Set employees;

Dept class contains a Set of employees. This is how foreign key representation of tables is represented in bean classes.

After knowing this much, let us go into the programming.

Create two tables (with following SQL commands) dept and employee and keep ready.

SQL> create table dept (dept_id integer, dept_name varchar(15), primary key (dept_id));

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

Note: "dept_id integer" must exist as foreign key in employee table to which data will be fed from dept table. This we do it in mapping XML file

Now, Write the two beans.

1. File Name:

private Set employees;

As Dept may contain many employees, all the employees present in the dept are created as Set and added to Dept class. This is how one to many association of tables is implemented in Java classes. Here one is Dept class (represented by dept table) and many is Employee (represented by employee table). Set is preferred as one employee id cannot be duplicated (if duplicated, as in some cases (one person can order the same item number of times), List can be preferred)).

This is represented in mapping file as follows:

set name="employees": The value of name attribute should be the reference variable of Set declared in Dept class (private Set employees). A novice gets confusion here only.

The <key> element in mapping file represents always a foreign key. Here, the foreign key is dept_id declared in employee table. As per this foreign key dept_id, the Set elements of Dept class are populated into employee table. This must be very clear as the whole logic of association lies here.

cascade=”persist, delete”

The cascade attribute populates the data to other table records. The cascade, here, works only for persist (inserting) and delete (deleting) operations. If cascading is required for all the operations, write as follows:


2. File Name:

3. File Name: hibernate.cfg.xml

<property name="">update</property>

Actually, the above statement is not required as we have created table manually. I prefer to have as a safety measure when a column is missed, it is created implicitly.

4. File Name: dept.hbm.xml

5. Client program:

Transaction trans = session.getTransaction();

The above two statements can be replaced as

Transaction trans = session.beginTransaction();

Three Employee objects emp1, emp2 and emp3 are created and set some values. These objects are added to Set emps. emps is added to Dept object dept. The dept object, at runtime, adds its own values like deptName etc. to its own dept table and the other employeeId and employeeName etc. to employee table.

Observe the tables output.


Following is the Console screenshot.

One-to-Many Hibernate

Now let us write one more client program, Client2 to fetch the records of dept and employee tables through one dept object.

One-to-Many Hibernate

Now let us write one more client program, DeleteClient to delete one record. When one Dept object is deleted, it’s associated all the Employee objects are also deleted.

After executing the above deletion program, check the database, the record of 1234 of dept table and its associated records of employee table are deleted. This is the power of Hibernate. Is it so simple in JDBC?

Leave a Reply

Your email address will not be published. Required fields are marked *