Javatpoint Logo
Javatpoint Logo

JPA Map Mapping

A Map is an interface in which a unique key is associated with each value object. Thus, operations like search, update, delete are performed on the basis of key.

Map Mapping Example

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

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

  • Now, create a class of embedded object Address.java under com.javatpoint.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 MapMapping.java under com.javatpoint.collection package to persist the entity object with data.

MapMapping.java

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.

JPA Map Mapping
  • Employee_map 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_map query in MySQL.

JPA Map Mapping





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA