407
JPA List Mapping
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.
This example contains the following steps: –
- Create an entity class Employee.java under com.tutoraspire.jpa package that contains employee id, name and embedded object (employee Address). The annotation @ElementCollection represents the embedded object.
Employee.java
- Now, create a class of embedded object Address.java under com.tutoraspire.jpa package. The annotation @Embeddable represents the embeddable object.
Address.java
- Now, map the entity class and other databases confiuguration in Persistence.xml file.
Persistence.xml
- Create a persistence class ListMapping.java under com.tutoraspire.collection package to persist the entity object with data.
ListMapping.java
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.
Next TopicJPA Set Mapping