Hibernate one to many mapping is made between two entities where the first entity can have a relation with multiple instances of the second entity but the second can be associated with only one instance of the first entity. It is a 1 to N relationship. For example, …
Hibernate one to many mapping is made between two entities where the first entity can have a relation with multiple instances of the second entity but the second can be associated with only one instance of the first entity. It is a 1 to N relationship.
For example, in any company, an employee can register for multiple bank accounts but one bank account will be associated with one and only one employee. In this hibernate one to many mapping annotation examples, we will learn to make such mappings in the database using hibernate.
1. Design Overview
We should use one to many mapping to create 1..N relationship between entities or objects.
For example, as discussed above, we have to write two entities i.e. EmployeeEntity and AccountEntity such that multiple accounts can be associated with a single employee, but one single account can not be shared between two or more employees.
This problem can be solved in two different ways.
One is to have a foreign key column in the ACCOUNT table i.e. EMPLOYEE_ID. This column will refer to the primary key of Employee table. This way no two accounts can be associated with multiple employees. Obviously, the account number needs to be unique for enforcing this restriction.
The second approach is to have a link table. Let’s say the table name is EMPLOYEE_ACCOUNT. This table will have two columns i.e. EMP_ID that will be a foreign key referring to the primary key in EMPLOYEE table and similarly ACCOUNT_ID which will be a foreign key referring to the primary key of ACCOUNT table.
2. Using Foreign Key Association
We are designing a unidirectional relationship where when we delete an employee then account are deleted as well. But when we delete an account (one of many) then employee is unaffected.
2.1. Create Association
Let’s first see the schema design.
Then we write the entity classes.
EmployeeEntity.java
@Entity@Table(name ="EMPLOYEE")publicclassEmployeeEntityimplementsSerializable{@Id@GeneratedValue(strategy =GenerationType.IDENTITY)@Column(name ="ID")privateInteger employeeId;@OneToMany(cascade=CascadeType.ALL)@JoinColumn(name="EMPLOYEE_ID")privateSet<AccountEntity> accounts;//Other fields, getters, setters are hidden for brevity}
AccountEntity.java
@Entity@Table(name ="ACCOUNT")publicclassAccountEntityimplementsSerializable{@Id@GeneratedValue(strategy =GenerationType.IDENTITY)@Column(name ="ID")privateInteger accountId;//Other fields, getters, setters are hidden for brevity}
2.2. Demo
Let’s test the relationship and monitor the CREATE queries.
create table Employee(
ID integer generated by default as identity,EMAILvarchar(100) not null,FIRST_NAMEvarchar(100) not null,LAST_NAMEvarchar(100) not null,
primary key (ID))
create table ACCOUNT (
ID integer generated by default as identity,ACC_NUMBERvarchar(100) not null,EMPLOYEE_ID integer,
primary key (ID))
alter table if exists ACCOUNT
add constraint FKmyqrmihkv5isa3tjsj01x65sr
foreign key (EMPLOYEE_ID)
references Employee
Hibernate: insert into Employee (EMAIL, FIRST_NAME, LAST_NAME, ID) values (?,?,?,?)Hibernate: insert into ACCOUNT (ACC_NUMBER, ID) values (?,?)Hibernate: insert into ACCOUNT (ACC_NUMBER, ID) values (?,?)Hibernate: insert into EMPLOYEE_ACCOUNT (EMPLOYEE_ID, ACCOUNT_ID) values (?,?)Hibernate: insert into EMPLOYEE_ACCOUNT (EMPLOYEE_ID, ACCOUNT_ID) values (?,?)
In this hibernate @OneToMany mapping annotation example using list, we learned to create 1..N relationships between two entities using foreign key association and join table techniques.
A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Comments