Pages Navigation Menu

Coding is much easier than you think

Saving Collections in Hibernate

saving collections
In our last Hibernate article, we learned about Embedding value object into an entity class. In this article we’re going to focus on saving a Collection in Hibernate.
Consider a scenario where the student have lots of address. So in this case we need to create more no of embedded object in the student class, which is so tedious at certain point. In such case we can use Collection to solve this problem. This collection can be a list, set, map, collection, sorted set, sorted map. Hibernate provides the facility to persist the collections object via @ElementCollection annotation.

Entity Class


package entity;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "STUDENT")
public class Student {

	@Column(name = "ID")
	private int id;

	@Column(name = "NAME")
	private String name;

	@Column(name = "DEPARTMENT")
	private String department;

	@Column(name = "COLLEGE")
	private String college;

	private Collection<Address> lisOfAddress = new ArrayList<Address>();

        // Create Getters and Setters



Defines a collection of instances of an embeddable class, this annotation must be specified if the collection is to be mapped by means of a collection table.

package entity;

import javax.persistence.Column;
import javax.persistence.Embeddable;

public class Address {
	@Column(name = "STREET_NAME")
	private String street;
	@Column(name = "CITY_NAME")
	private String city;
	@Column(name = "STATE_NAME")
	private String state;
	@Column(name = "PIN_CODE")
	private String pincode;
        // Create Getters and Setters

hibernate.cfg.xml will be the same as in previous article.

HibernateTest class

Create the Main class to run the example.

package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import entity.Address;
import entity.Student;

public class HibernateUtil {

	public static void main(String[] args) {

		Configuration cf = new Configuration().configure("hibernate.cfg.xml");

		StandardServiceRegistryBuilder srb = 
                               new StandardServiceRegistryBuilder();
		ServiceRegistry sr =;
		SessionFactory sf = cf.buildSessionFactory(sr);

		Session session = sf.openSession();
		Transaction tx = session.beginTransaction();
		Address address1 = new Address(); 
		address1.setStreet("Race cource");
		Address address2 = new Address();
		address2.setStreet("Besant nagar");
		Student student = new Student(); 

		student.setName("Lahir Nisha");
		//adding addresses object to the list of address



Rut it – Eclipse Console

When you run the above program two tables are created in database, first table is for Student entity “STUDENT” and second table is for the embeddable object (Address) – “Student_lisOfAddress” (The default table name for this table gets generated based on – Entity class name_Name of the embeddable object in the entity class).
Here the ‘Student_ID’ column in ‘Student_lisOfAddress’ is the foreign key reference of Student tables ‘ID’. In next article we shall learn to override the default collection class table name and to add primary key to it.

About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

  • john

    Why we are adding addresses object to the list of address using getLisOfAddress() instead of setLisOfAddress()