Map Mapping

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.

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

This example contains the following steps: -

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

Employee.java

package com.javahubpoint.jpa;  
import java.util.*;
import javax.persistence.*;

@Entity
public class Employee {  
  
    @Id  
    @GeneratedValue(strategy=GenerationType.AUTO)  
    private int e_id;  
    private String e_name;  
      
    @ElementCollection  
    private Map<Integer,Address> map=new HashMap<Integer,Address>();  
  
      
    public int getE_id() {  
        return e_id;  
    }  
  
    public void setE_id(int e_id) {  
        this.e_id = e_id;  
    }  
  
    public String getE_name() {  
        return e_name;  
    }  
  
    public void setE_name(String e_name) {  
        this.e_name = e_name;  
    }  
  
    public Map<Integer, Address> getMap() {  
        return map;  
    }  
  
    public void setMap(Map<Integer, Address> map) {  
        this.map = map;  
    }    
}  

Step 2.  Now, create a class of embedded object Address.java under com.javahubpoint.jpa package. The annotation @Embeddable represents the embeddable object.

Address.java

package com.javahubpoint.jpa;  
import javax.persistence.*;  
  
@Embeddable  
public class Address {  
  
    private int e_pincode;  
    private String e_city;  
    private String e_state;  
    public int getE_pincode() {  
        return e_pincode;  
    }  
    public void setE_pincode(int e_pincode) {  
        this.e_pincode = e_pincode;  
    }  
    public String getE_city() {  
        return e_city;  
    }  
    public void setE_city(String e_city) {  
        this.e_city = e_city;  
    }  
    public String getE_state() {  
        return e_state;  
    }  
    public void setE_state(String e_state) {  
        this.e_state = e_state;  
    } 
}  

Step 3.  Now, map the entity class and other databases configuration in Persistence.xml file.

Persistence.xml

<persistence>  
<persistence-unit name="Collection_Type">  
      
     <class>com.javahubpoint.jpa.Employee</class>  
      <class>com.javahubpoint.jpa.Address</class> 
        
     <properties>  
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>  
         <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/collection_mapping"/>  
         <property name="javax.persistence.jdbc.user" value="root"/>  
         <property name="javax.persistence.jdbc.password" value=""/>  
         <property name="eclipselink.logging.level" value="SEVERE"/>  
         <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>  
      </properties>  
      
    </persistence-unit>  
</persistence>  

Step 4. Create a persistence class MapMapping.java under com.javahubpoint.collection package to persist the entity object with data.

MapMapping.java

package com.javahubpoint.collection;  
import javax.persistence.*;
import com.javahubpoint.jpa.*; 
 
public class ListMapping{  
  
    public static void main(String[] args) {  
          
        EntityManagerFactory emf=Persistence.createEntityManagerFactory("Collection_Type");  
        EntityManager em=emf.createEntityManager();  
          
        em.getTransaction().begin();
          
        Address a1=new Address();  
        a1.setE_pincode(201301);  
        a1.setE_city("Noida");  
        a1.setE_state("Uttar Pradesh");  
       
        Address a2=new Address();  
        a2.setE_pincode(302001);  
        a2.setE_city("Jaipur");  
        a2.setE_state("Rajasthan");  
          
        Address a3=new Address();  
        a3.setE_pincode(133301);  
        a3.setE_city("Chandigarh");  
        a3.setE_state("Punjab");  
      
        Address a4=new Address();  
        a4.setE_pincode(80001);  
        a4.setE_city("Patna");  
        a4.setE_state("Bihar");  
          
   
    Employee e1=new Employee();  
    e1.setE_id(1);  
    e1.setE_name("Vijay");  
      
    Employee e2=new Employee();  
    e2.setE_id(2);  
    e2.setE_name("Vijay");  
      
    Employee e3=new Employee();  
    e3.setE_id(3);  
    e3.setE_name("William");  
      
    Employee e4=new Employee();  
    e4.setE_id(4);  
    e4.setE_name("Rahul");  
      
    e1.getMap().put(1, a1);  
    e2.getMap().put(2, a2);  
    e3.getMap().put(3, a3);  
    e4.getMap().put(4, a4);  
      
    em.persist(e1);  
    em.persist(e2);  
    em.persist(e3);  
    em.persist(e4);  
      
    em.getTransaction().commit();  
      
    em.close();  
    emf.close();    
    }
}  

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.







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.