One-to-Many Mappings


One-to-Many Mappings

A Set java collection that doesn't have any duplicate elements can implement a One-to-Many mapping. We've already seen how to map a Set collection in Hibernate, so if you've already mastered Set mapping, the one-to-many mapping should be a breeze.

A Set is mapped with a element in the mapping table and initialized with java.util.HashSet. You can use the Set collection in your class when there is no duplicate element required in the collection.

Hibernate provides us to represent the entities with relationships. To implement one to many relationships between the two components. We need to define the association relationship between the two classes, and we need to have the dependency of the child class in the parent class.

A typical example for one to many relationships is, Customer and Items, i.e., a customer can order multiple items. Here the relationship between Customer to Item is one to many relationships.

create table EMPLOYEE (
id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
PRIMARY KEY (id)