Hibernate - Map Mapping
The 'Map' mapping is also same as 'List' and 'Set' mapping with minor changes. As we know a 'Map' contains a key and a value. So, we will be making changes in our code accordingly.
class Employee{
int id;
String name;
Map<String,Address> addressMap = new HashMap<String,Address> ();
---Getters & Setters---
}
We have used 'Map' to hold multiple 'Address'.
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 'Map'.
<?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 = "id" type = "int">
<column name = "ID">
</id>
<property name = "name" type = "string">
<column name = "NAME">
</property>
<map name="addressSet" table="ADDRESS">
<key column="EMP_ID"/>
<map-key type="string" column="MAP_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>
</map>
</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 'Map' of java to achieve that.
<map name="addressSet" 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 'Map'. Since, a 'Map' manages its data in key and value pair, we need an extra column in database to store the key of the Map as well. So, we have used the <map-key> tag to create a column 'MAP_COUNTER' which will be keeping a track of the map keys in the Database side.
<map-key type="string" column="MAP_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();
Map<String,Address> addressMap = new HashMap<Address> ();
Address localAddress = new Address();
localAddress.setStreetName("Walls Street");
localAddress.setCity("Delhi");
localAddress.setPinCode("110012");
addressMap.put("Local Address",localAddress);
Address permanentAddress = new Address();
permanentAddress.setStreetName("Murugeshpalya");
permanentAddress.setCity("Bangalore");
permanentAddress.setPinCode("560019");
addressMap.put("Permanent Address",permanentAddress);
Employee employee = new Employee();
employee.setId(1);
employee.setName("Joe");
employee.setAddressMap(addressMap);
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 'Map'. We have created a 'Map' type object
Map<String,Address> addressMap = new HashMap<Address> ();
to store the 'localAddress' where we have made "Local Address" as the key
addressMap.put("Local Address",localAddress);
and 'permanentAddress' where we have made "Permanent Address" as the key.
addressMap.put("Permanent Address",permanentAddress);
And finally we need to add the Map 'addressMap' to the 'Employee' object.
employee.setAddressMap(addressMap);
EMPLOYEE
ADDRESS
MAP_COUNTER |
ID |
STREET_NAME |
CITY |
PIN_CODE |
Local Address |
1 |
Walls street |
Delhi |
110012 |
Permanent Address |
1 |
Murugeshpalya |
Bangalore |
560019 |
If you see the above table, a new column 'MAP_COUNTER' is created in the 'ADDRESS' table which keeps track of the map key.