Set Mapping

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.

private Set<Address> address=new HashSet<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 Set<Address> address=new HashSet<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 Set<Address> getAddress() {  
        return address;  
    }  
  
    public void setAddress(Set<Address> address) {  
        this.address = address;  
    }
}

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 ListMapping.java under com.javahubpoint.collection package to persist the entity object with data.

SetMapping.java

package com.javahubpoint.collection;  
import javax.persistence.*;
import com.javahubpoint.jpa.*;

public class SetMapping{  
  
    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.getAddress().add(a1);  
    e2.getAddress().add(a2);  
    e3.getAddress().add(a3);  
    e4.getAddress().add(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_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.