- To persist Map of embeddable keys and basic types values, the annotation
@ElementCollection is used on the map field/property.
- The Entity and the Map are mapped to two separate foreign/primary-key tables. One for the entity and other table (join table) for embeddable fields/properties (as map key) along with a single column for the map value. The Map table has the foreign key pointing to the primary key of the entity. Also all columns corresponding to the embeddable fields/properties will have primary key constraint.
- This is similar to mapping a Map of basic type keys and @Embeddable values, the only difference is the map key is mapped to multiple columns (depending on number of embeddable fields) instead of just one in this case.
- In this case the embeddable class must implement the hashCode and equals methods (a general requirement for map keys).
- By default, JPA naming conventions are used for the mapping. We can customize that by using @JoinTable and/or @Column annotations.
Example
This is a very simple example with default table/columns mapping. At the end we will see more examples.
@Entity
public class Customer {
@Id
@GeneratedValue
private int id;
private String name;
@ElementCollection
private Map<Order, String> orders;
.............
}
@Embeddable
public class Order {
private String item;
private int qty;
private Date date;
.............
}
public class ExampleMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("example-unit");
try {
EntityManager em = emf.createEntityManager();
nativeQuery(em, "SHOW TABLES");
nativeQuery(em, "SHOW COLUMNS from Customer");
nativeQuery(em, "SHOW COLUMNS from CUSTOMER_ORDERS");
} finally {
emf.close();
}
}
.............
}Output'SHOW TABLES' [CUSTOMER, PUBLIC] [CUSTOMER_ORDERS, PUBLIC] 'SHOW COLUMNS from Customer' [ID, INTEGER(10), NO, PRI, NULL] [NAME, VARCHAR(255), YES, , NULL] 'SHOW COLUMNS from CUSTOMER_ORDERS' [CUSTOMER_ID, INTEGER(10), NO, PRI, NULL] [ORDERS, VARCHAR(255), YES, , NULL] [DATE, TIMESTAMP(23), NO, PRI, NULL] [ITEM, VARCHAR(255), NO, PRI, NULL] [QTY, INTEGER(10), NO, PRI, NULL]
H2 database SHOW statements
A quick overview of the mapping:
Persisting and loading data
public class ExampleMain2 {
public static void main(String[] args) {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("example-unit");
try {
persistEntity(emf);
runNativeQueries(emf);
loadEntity(emf);
} finally {
emf.close();
}
}
private static void persistEntity(EntityManagerFactory emf) {
System.out.println("-- Persisting entities --");
EntityManager em = emf.createEntityManager();
Customer c1 = new Customer();
c1.setName("Lindsey Craft");
c1.addOrder("online", "XYZ Blender", 2);
c1.addOrder("store", "ZZZ Beer Glass", 4);
System.out.println(c1);
Customer c2 = new Customer();
c2.setName("Morgan Philips");
c2.addOrder("online", "AA Glass Cleaner", 3);
System.out.println(c2);
em.getTransaction().begin();
em.persist(c1);
em.persist(c2);
em.getTransaction().commit();
em.close();
}
private static void runNativeQueries(EntityManagerFactory emf) {
System.out.println("-- Native queries --");
EntityManager em = emf.createEntityManager();
ExampleMain.nativeQuery(em, "Select * from Customer");
ExampleMain.nativeQuery(em, "Select * from Customer_Orders");
}
private static void loadEntity(EntityManagerFactory emf) {
System.out.println("-- Loading Customer --");
EntityManager em = emf.createEntityManager();
List<Customer> entityAList = em.createQuery("Select t from Customer t")
.getResultList();
entityAList.forEach(System.out::println);
em.close();
}
}Output-- Persisting entities -- Customer{id=0, name='Lindsey Craft', orders={Order{item='ZZZ Beer Glass', qty=4, date=Wed Oct 29 13:18:20 CST 2025}=store, Order{item='XYZ Blender', qty=2, date=Wed Oct 29 13:18:20 CST 2025}=online}} Customer{id=0, name='Morgan Philips', orders={Order{item='AA Glass Cleaner', qty=3, date=Wed Oct 29 13:18:20 CST 2025}=online}} -- Native queries -- 'Select * from Customer' [1, Lindsey Craft] [2, Morgan Philips] 'Select * from Customer_Orders' [1, store, 2025-10-29 13:18:20.23, ZZZ Beer Glass, 4] [1, online, 2025-10-29 13:18:20.23, XYZ Blender, 2] [2, online, 2025-10-29 13:18:20.236, AA Glass Cleaner, 3] -- Loading Customer -- Customer{id=1, name='Lindsey Craft', orders={Order{item='ZZZ Beer Glass', qty=4, date=2025-10-29 13:18:20.23}=store, Order{item='XYZ Blender', qty=2, date=2025-10-29 13:18:20.23}=online}} Customer{id=2, name='Morgan Philips', orders={Order{item='AA Glass Cleaner', qty=3, date=2025-10-29 13:18:20.236}=online}}
Example ProjectDependencies and Technologies Used: - h2 1.4.194: H2 Database Engine.
- hibernate-core 5.2.10.Final: The core O/RM functionality as provided by Hibernate.
Implements javax.persistence:javax.persistence-api version 2.1 - JDK 1.8
- Maven 3.3.9
|
|