Hibernate @ElementCollection and @CollectionTable Annotation

In this post, we will learn about map a collection of basic type using Hibernate @ElementCollection and @CollectionTable annotation.

Learn about developing CRUD application using Spring MVC and Hibernate




 

Imagine the scenario


We are creating a Employee Tracking Application, each employee can have multiple phone numbers and multiple address. In order to persist the employee data we need to create database table for storing phone numbers and addresses.

Look at the above database design, we have 3 tables, employee, phone number and address. Both the employee_phone_number and employee_address table contains employee_id which is foreign key to the employee table.

We can easily achieve this by implementing an object level mapping which One-to-Many relationship which is provided by JPA/Hibernate. But if you observe carefully, phone number is a String type and address is Embeddable type. So if you have a basic type like String, Integer, Double or an embeddable type like address then Hibernate has provided a solution in the form of @ElementCollection and @CollectionTable annotation.

Important Annotations


Annotation Description
@ElementCollection It marks as map to a collection. All the records of the collection are stored in a seperate table.
@CollectionTable Specify the name of the table in which all the collection information is stored.
@JoinColumn It refers to the primary table column.

Understand with Example


Now let’s understand @ElementCollection and @CollectionTable annotation with an example




Tools and Technologies Used


  • Hiberante – 5.4.4.Final
  • MySQL – 5.1.47
  • JDK – 1.8 or later
  • Maven – 3.1
  • IDE – Eclipse oxygen

Development Steps


  1. Create a maven project
  2. Add the required dependencies – pom.xml
  3. Project structure
  4. Configure hibernate
  5. Create model class – EmployeeModel.java
  6. Develop the main application – ElementCollectionDemo.java
  7. Run and Test the application

 

1. Create a maven quickstart project


In eclipse go to File -> New -> Select other as shown in the below image

Search for maven project as shown in the below image,

Select maven project and click next,

Search for maven quickstart archetype as shown in the image,

Enter the details as shown the below image,

Group Id: in.bushansirgur
Artifact Id: hibernate-advance-concept-01
Version: 0.0.1-SNAPSHOT
Package: in.bushansirgur.hibernate




2. Add the required dependencies


Let’s define all the required dependencies in pom.xml to develop this application.

3. Project structure


Following is the project structure for your reference.

4. Configure hibernate


Create hibernate.cfg.xml file inside the resources folder and define the jdbc connection properties and hibernate properties.

5. Create model class


Let’s create EmployeeModel model class inside the in.bushansirgur.hibernate.model package which will mapped to the employee table in the database




6. Develop the main application


Let’s create ElementCollectionDemo class that defines the main method. Create employee object and add the phone numbers that the employee have. Finally we will persist the employee object and commit on the current transaction.

7. Run and Test the application


After the run the application check the console, you will see the following output

Go ahead and check all the tables in MySQL. The employee table will have a new entry and table employee_phone_numbers will have 3 entries.

Conclustion


That’s all for the article. I hope you have learned something in this article.

You can find the entire source code for the project in my Github repository.

Github repository





Download source code of this project https://github.com/scbushan05/hibernate-advance-concept-01

Happy coding!

About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

Leave a Reply

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