Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Component - Set Mapping using Annotations

If we use annotations, we do not have to write the extra mapping files for 'Employee' and 'Address'. We can configure the mapping in the Class itself.

Note : While dealing with components we have put the 'Employee' record and its corresponding 'Address' record in the same table i.e. EMPLOYEE table. But in this case we can have multiple 'Address' for a single 'Employee'. Although, 'Address' is a Component still we need to have two different tables i.e 'EMPLOYEE' AND 'ADDRESS' to avoid data duplication.

Let us see the example below:

So, an 'Employee' can have multiple 'Address'.


class Employee{
    int id;
    String name;

    @ElementCollection
    @JoinTable(name="ADDRESS",JoinColumns=@JoinColumn(name="ID"))
    Set <Address> addressSet = new HashSet <Address> ();
    ---Getters & Setters---
 }


We have used 'Set' to hold multiple 'Address'.

Set <Address> addressSet = new HashSet <Address> ();

@ElementCollection above the 'Set' tells Hibernate that a separate 'ADDRESS' table should be created.
And the

@JoinTable(name="ADDRESS", JoinColumns=@JoinColumn(name="ID"))


tells Hibernate that the name of the table should be 'ADDRESS' and the foreign key should be named as 'ID'.


Similarly, the 'Address' class looks like :


class Address{
    String streetName;
    String city;
    String pinCode;
    ---Getters & Setters---
 }

Next, let us write the main class where we will be saving the 'Employee' data and its corresponding 'Address' data to the database using Hibernate.


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSave {
   public static void main(String[] args) {

    static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Set<Address> addressSet = new HashSet<Address> ();

    Address localAddress = new Address();
    localAddress.setStreetName("Walls Street");
    localAddress.setCity("Delhi");
    localAddress.setPinCode("110012");

    addressSet.add(localAddress);

    Address permanentAddress = new Address();
    permanentAddress.setStreetName("Murugeshpalya");
    permanentAddress.setCity("Bangalore");
    permanentAddress.setPinCode("560019");

    addressSet.add(permanentAddress);

    Employee employee = new Employee();
    employee.setId(1);
    employee.setName("Joe");
    employee.setAddressSet(addressSet);
    session.save(employee);

    session.getTransaction().commit();

    session.close();
    sessionFactory().close();
  }
}

Below is the output:


EMPLOYEE

ID NAME
1 Joe

ADDRESS

ID STREET_NAME CITY PIN_CODE
1 Walls street Delhi 110012
1 Murugeshpalya Bangalore 560019