MAP mapping in JPA

A Map is an interface in which a unique key is associated with each value object. Thus, operations like search, update, delete are performed on the basis of key.

Map Mapping Example

In this example, we embed an object in an entity class and define it as a collection type Map.

  1. private Map<Integer,Address> map=new HashMap<Integer,Address>();  

This example contains the following steps: –

  • Create an entity class Employee.java under com.javatpoint.jpa package that contains employee id, name and embedded object (employee Address). The annotation @ElementCollection represents the embedded object.

Employee.java

  1. package com.javatpoint.jpa;  
  2. import java.util.*;  
  3.   
  4. import javax.persistence.*;  
  5. @Entity  
  6.   
  7. public class Employee {  
  8.   
  9.     @Id  
  10.     @GeneratedValue(strategy=GenerationType.AUTO)  
  11.     private int e_id;  
  12.     private String e_name;  
  13.       
  14.     @ElementCollection  
  15.     private Map<Integer,Address> map=new HashMap<Integer,Address>();  
  16.   
  17.       
  18.     public int getE_id() {  
  19.         return e_id;  
  20.     }  
  21.   
  22.     public void setE_id(int e_id) {  
  23.         this.e_id = e_id;  
  24.     }  
  25.   
  26.     public String getE_name() {  
  27.         return e_name;  
  28.     }  
  29.   
  30.     public void setE_name(String e_name) {  
  31.         this.e_name = e_name;  
  32.     }  
  33.   
  34.     public Map<Integer, Address> getMap() {  
  35.         return map;  
  36.     }  
  37.   
  38.     public void setMap(Map<Integer, Address> map) {  
  39.         this.map = map;  
  40.     }  
  41.   
  42.       
  43.       
  44. }  
  • Now, create a class of embedded object Address.java under com.javatpoint.jpa package. The annotation @Embeddable represents the embeddable object.

Address.java

  1. package com.javatpoint.jpa;  
  2. import javax.persistence.*;  
  3.   
  4. @Embeddable  
  5. public class Address {  
  6.   
  7.     private int e_pincode;  
  8.     private String e_city;  
  9.     private String e_state;  
  10.     public int getE_pincode() {  
  11.         return e_pincode;  
  12.     }  
  13.     public void setE_pincode(int e_pincode) {  
  14.         this.e_pincode = e_pincode;  
  15.     }  
  16.     public String getE_city() {  
  17.         return e_city;  
  18.     }  
  19.     public void setE_city(String e_city) {  
  20.         this.e_city = e_city;  
  21.     }  
  22.     public String getE_state() {  
  23.         return e_state;  
  24.     }  
  25.     public void setE_state(String e_state) {  
  26.         this.e_state = e_state;  
  27.     }  
  28.       
  29.       
  30.       
  31. }  
  • Now, map the entity class and other databases confiuguration in Persistence.xml file.

Persistence.xml

  1. <persistence>  
  2. <persistence-unit name=”Collection_Type”>  
  3.       
  4.      <class>com.javatpoint.jpa.Employee</class>  
  5.       <class>com.javatpoint.jpa.Address</class>  
  6.       
  7.   
  8.         
  9.      <properties>  
  10.       <property name=”javax.persistence.jdbc.driver” value=”com.mysql.jdbc.Driver”/>  
  11.          <property name=”javax.persistence.jdbc.url” value=”jdbc:mysql://localhost:3306/collection_mapping”/>  
  12.          <property name=”javax.persistence.jdbc.user” value=”root”/>  
  13.          <property name=”javax.persistence.jdbc.password” value=””/>  
  14.          <property name=”eclipselink.logging.level” value=”SEVERE”/>  
  15.          <property name=”eclipselink.ddl-generation” value=”create-or-extend-tables”/>  
  16.       </properties>  
  17.       
  18.     </persistence-unit>  
  19. </persistence>  
  • Create a persistence class MapMapping.java under com.javatpoint.collection package to persist the entity object with data.

MapMapping.java

  1. package com.javatpoint.collection;  
  2. import javax.persistence.*;  
  3.   
  4.   
  5. import com.javatpoint.jpa.*;  
  6. public class ListMapping{  
  7.   
  8.     public static void main(String[] args) {  
  9.           
  10.         EntityManagerFactory emf=Persistence.createEntityManagerFactory(“Collection_Type”);  
  11.         EntityManager em=emf.createEntityManager();  
  12.           
  13.         em.getTransaction().begin();  
  14.           
  15.           
  16.   
  17.           
  18.         Address a1=new Address();  
  19.         a1.setE_pincode(201301);  
  20.         a1.setE_city(“Noida”);  
  21.         a1.setE_state(“Uttar Pradesh”);  
  22.           
  23.           
  24.           
  25.           
  26.           
  27.         Address a2=new Address();  
  28.         a2.setE_pincode(302001);  
  29.         a2.setE_city(“Jaipur”);  
  30.         a2.setE_state(“Rajasthan”);  
  31.           
  32.         Address a3=new Address();  
  33.         a3.setE_pincode(133301);  
  34.         a3.setE_city(“Chandigarh”);  
  35.         a3.setE_state(“Punjab”);  
  36.       
  37.         Address a4=new Address();  
  38.         a4.setE_pincode(80001);  
  39.         a4.setE_city(“Patna”);  
  40.         a4.setE_state(“Bihar”);  
  41.           
  42.           
  43.     Employee e1=new Employee();  
  44.     e1.setE_id(1);  
  45.     e1.setE_name(“Vijay”);  
  46.       
  47.       
  48.     Employee e2=new Employee();  
  49.     e2.setE_id(2);  
  50.     e2.setE_name(“Vijay”);  
  51.       
  52.     Employee e3=new Employee();  
  53.     e3.setE_id(3);  
  54.     e3.setE_name(“William”);  
  55.       
  56.     Employee e4=new Employee();  
  57.     e4.setE_id(4);  
  58.     e4.setE_name(“Rahul”);  
  59.       
  60.     e1.getMap().put(1, a1);  
  61.     e2.getMap().put(2, a2);  
  62.     e3.getMap().put(3, a3);  
  63.     e4.getMap().put(4, a4);  
  64.       
  65.     em.persist(e1);  
  66.     em.persist(e2);  
  67.     em.persist(e3);  
  68.     em.persist(e4);  
  69.       
  70.     em.getTransaction().commit();  
  71.       
  72.     em.close();  
  73.     emf.close();  
  74.           
  75.           
  76.           
  77.     }  
  78.       
  79. }  

Output:

After the execution of the program, the following tables are generated under MySQL workbench.

  • Employee table – This table contains the employee details. To fetch data, run select * from employee query in MySQL.
JPA Map Mapping
  • Employee_map table – This table represents the mapping between employee and address table. The data in the table is arranged in an unordered manner.To fetch data, run select * from employee_map query in MySQL.
JPA Map Mapping

Leave a Comment