Hibernate - List Mapping
The List mapping is almost same as Set mapping with minor changes.
class Employee{
int id;
String name;
List <Address> addressSet = new ArrayList <Address>();
---Getters & Setters---
}
We have used 'List' to hold multiple 'Address'. And the 'List' is going to maintain the order of the values inserted.
Let us follow the below steps:
1. Firstly in the mapping file we need to tell Hibernate that the 'Address' class has to be treated as a 'List'.
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "Employee" table = "EMPLOYEE">
<id name = "empId" type = "int">
<column name = "ID">
</id>
<property name = "name" type = "string">
<column name = "NAME">
</property>
<list name="addressList" table="ADDRESS">
<key column="EMP_ID"/>
<list-index column="COUNTER">
<composite-element class="Address">
<property name = "streetName" type = "string">
<column name = "STREET_NAME">
</property>
<property name = "city" type = "string">
<column name = "CITY">
</property>
<property name = "pinCode" type = "string">
<column name = "PIN_CODE">
</property>
</composite-element>
</list>
</class>
</hibernate-mapping>
In the above statement we have informed Hibernate that multiple 'Address' has to be added to the 'ADDRESS' table and we have used 'List' to achieve that.
<list name="addressList" table="ADDRESS">
Now, to create a link between the 'EMPLOYEE' and 'ADDRESS' table we have used the primary key 'ID' in the 'ADDRESS' table to establish a primary key - foreign key relationship.
<key column="ID"/>
The next line is specific to 'List'. Since, a 'List' is capable of managing the order of the values inserted, we need an extra column in database to maintain this order as well. So, we have used the tag to create a column 'COUNTER' which will be
keeping a track of the order in the Database side.
<list-index column="COUNTER">
Then we have informed Hibernate about the 'Address' class using the '<composite-element>' tag of Hibernate.
<composite-element class="Address">
<property name = "streetName" type = "string">
<column name = "STREET_NAME">
</property>
...
...
</composite-element>
2. Next, we will be writing 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();
List <Address> addressList = new ArrayList <Address>();
Address localAddress = new Address();
localAddress.setStreetName("Walls Street");
localAddress.setCity("Delhi");
localAddress.setPinCode("110012");
addressList.add(localAddress);
Address permanentAddress = new Address();
permanentAddress.setStreetName("Murugeshpalya");
permanentAddress.setCity("Bangalore");
permanentAddress.setPinCode("560019");
addressList.add(permanentAddress);
Employee employee = new Employee();
employee.setId(1);
employee.setName("Joe");
employee.setAddressList(addressList);
session.save(employee);
session.getTransaction().commit();
session.close();
sessionFactory().close();
}
}
So, in the above example we have created two 'Address'.
First is the local address of the Employee.
Address localAddress = new Address();
localAddress.setStreetName("Walls Street");
localAddress.setCity("Delhi");
localAddress.setPinCode("110012");
Second is the Permanent Address of the Employee.
Address permanentAddress = new Address();
permanentAddress.setStreetName("Murugeshpalya");
permanentAddress.setCity("Bangalore");
permanentAddress.setPinCode("560019");
And since the Addresses are stored in 'List'. We have created a 'List' type object
List <Address> addressList = new ArrayList <Address>();
to store the 'localAddress'
addressList.add(localAddress)
addressList.add(permanentAddress);
And finally we need to add the List 'addressList' to the 'Employee' object.
employee.setAddressList(addressList);
EMPLOYEE
ADDRESS
COUNTER |
ID |
STREET_NAME |
CITY |
PIN_CODE |
0 |
1 |
Walls street |
Delhi |
110012 |
1 |
1 |
Murugeshpalya |
Bangalore |
560019 |
If you see the above table, a new column 'COUNTER' is created in the 'ADDRESS' table which keeps track on the order using the index(Which is 0 for the first row and 1 for the second).