Set Mapping in JPA

A Set is an interface that contains unique elements. These elements don’t maintain any order. A Set can be used when there is a requirement of retrieving unique elements in an unordered manner.

Set Mapping Example

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

  1. private Set<Address> address=new HashSet<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 Set<Address> address=new HashSet<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 Set<Address> getAddress() {  
  35.         return address;  
  36.     }  
  37.   
  38.     public void setAddress(Set<Address> address) {  
  39.         this.address = address;  
  40.     }  
  41.   
  42.       
  43. }  
  • 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 ListMapping.java under com.javatpoint.collection package to persist the entity object with data.

SetMapping.java

  1. package com.javatpoint.collection;  
  2. import javax.persistence.*;  
  3.   
  4.   
  5. import com.javatpoint.jpa.*;  
  6. public class SetMapping{  
  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.getAddress().add(a1);  
  61.     e2.getAddress().add(a2);  
  62.     e3.getAddress().add(a3);  
  63.     e4.getAddress().add(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 Set Mapping
  • Employee_address 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_address query in MySQL.
JPA Set Mapping

Leave a Comment