JPA 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.
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.
- Now, create a class of embedded object Address.java under com.javatpoint.jpa package. The annotation @Embeddable represents the embeddable object.
- Now, map the entity class and other databases confiuguration in Persistence.xml file.
- Create a persistence class ListMapping.java under com.javatpoint.collection package to persist the entity object with data.
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.