List Mapping in JPA

A List is an interface which is used to insert and delete elements on the basis of index. It can be used when there is a requirement of retrieving elements in a user-defined order.

List Mapping Example

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

  1. private List<Address> address=new ArrayList<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 List<Address> address=new ArrayList<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 List<Address> getAddress() {  
  35.         return address;  
  36.     }  
  37.   
  38.     public void setAddress(List<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.

ListMapping.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.       
  33.           
  34.     Employee e1=new Employee();  
  35.     e1.setE_id(1);  
  36.     e1.setE_name(“Vijay”);  
  37.     e1.getAddress().add(a1);  
  38.       
  39.     Employee e2=new Employee();  
  40.     e2.setE_id(2);  
  41.     e2.setE_name(“John”);  
  42.     e2.getAddress().add(a2);  
  43.       
  44.     em.persist(e1);  
  45.     em.persist(e2);  
  46.       
  47.     em.getTransaction().commit();  
  48.       
  49.     em.close();  
  50.     emf.close();  
  51.           
  52.           
  53.           
  54.     }  
  55.       
  56. }  

Output:

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

  • Employee table – This table contains the employee details. To fetch data, run select * from employee query in MySQL.
JPA List Mapping
  • Employee_address table – This table represents the mapping between employee and address table. To fetch data, run select * from employee_address query in MySQL.
JPA List Mapping

Leave a Comment