Close

JPA - Persisting a Map of embeddable keys and basic values

[Updated: Jun 11, 2017, Created: Jun 10, 2017]
JPA - A quick overview of Map with embeddable keys and basic type values
  • 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='XYZ Blender', qty=2, date=Sat Jun 10 11:12:52 CDT 2017}=online, Order{item='ZZZ Beer Glass', qty=4, date=Sat Jun 10 11:12:52 CDT 2017}=store}}
Customer{id=0, name='Morgan Philips', orders={Order{item='AA Glass Cleaner', qty=3, date=Sat Jun 10 11:12:52 CDT 2017}=online}}
-- Native queries --
'Select * from Customer'
[1, Lindsey Craft]
[2, Morgan Philips]
'Select * from Customer_Orders'
[1, online, 2017-06-10 11:12:52.727, XYZ Blender, 2]
[1, store, 2017-06-10 11:12:52.727, ZZZ Beer Glass, 4]
[2, online, 2017-06-10 11:12:52.73, AA Glass Cleaner, 3]
-- Loading Customer --
Customer{id=1, name='Lindsey Craft', orders={Order{item='XYZ Blender', qty=2, date=2017-06-10 11:12:52.727}=online, Order{item='ZZZ Beer Glass', qty=4, date=2017-06-10 11:12:52.727}=store}}
Customer{id=2, name='Morgan Philips', orders={Order{item='AA Glass Cleaner', qty=3, date=2017-06-10 11:12:52.73}=online}}

Example Project

Dependencies 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

Map Embeddable Key Example Select All Download
  • map-with-embeddable-keys
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Customer.java
          • resources
            • META-INF

    Customizing mapping with @CollectionTable and @Column

    To customize map with embeddable keys mapping, we can use @CollectionTable to customize the join table with @CollectionTable. The column which maps to the map values (in the join table) can be customized by using @Column. Note that @MapKeyColumn has not effect, because there's no single column which points to the map key.

    @Entity
    public class Customer {
      @Id
      @GeneratedValue
      private int id;
      private String name;
    
      @ElementCollection
      @CollectionTable(name = "ORDERS", joinColumns = @JoinColumn(name = "CUST_ID"))
      @Column(name = "ORDER_TYPE")
      private Map<Order, String> orders;
        .............
    }
    @Embeddable
    public class Order {
      private String item;
      private int qty;
      private Date date;
        .............
    }
    Complete Example

    See Also