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.
private List<Address> address=new ArrayList<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
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 List<Address> address=new ArrayList<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 List<Address> getAddress() {
return address;
}
public void setAddress(List<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
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-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.
ListMapping.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");
a2.setE_city("Jaipur");
a2.setE_state("Rajasthan");
Employee e1=new Employee();
e1.setE_id(1);
e1.setE_name("Vijay");
e1.getAddress().add(a1);
Employee e2=new Employee();
e2.setE_id(2);
e2.setE_name("John");
e2.getAddress().add(a2);
em.persist(e1);
em.persist(e2);
em.getTransaction().commit();
em.close();
emf.close();
}
}
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.
Employee_address table - This table represents the mapping between employee and address table. To fetch data, run select * from employee_address query in MySQL.