JPA - Persisting Collections of embeddable type by using @ElementCollection

[Updated: May 31, 2017, Created: May 26, 2017]
A quick overview of collection of embeddable types in JPA
  • Just like the collection of basic type, a collection of embeddable class can be persisted by using @ElementCollection on the collection reference in the entity class.
  • The corresponding class of the collection elements, must have @Embeddable annotation.
  • The Entity and the embeddable collection are mapped to two separate foreign/primary-key tables. One for the entity and other for embeddable fields/properties values. The collection table has the foreign key pointing to the primary key of the entity.
  • This is similar to OneToMany, except the target is an embeddable class instead of another entity.
  • By default, JPA specific naming conventions are used for the mapping. To customized that, we can use @CollectionTable annotation along with @ElementCollection.
  • Note that all cases of @Embeddable class are applied here: we can use an embeddable class within another one, the embeddable can have entity relationship and/or we can also use multiple instances of same embeddable type collection in our entity.

Example

@Entity
public class Customer {
  @Id
  @GeneratedValue
  private int id;
  private String name;
  @ElementCollection
  private List<PhoneNumber> phoneNumbers;
    .............
}
@Embeddable
public class PhoneNumber {
  private String phoneNumber;
  @Enumerated(EnumType.STRING)
  private PhoneType type;
    .............
}
public enum PhoneType {
  House, Work, Cell
}
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_PhoneNumbers");

          emf.close();
      } finally {
          emf.close();
      }
  }

  public static void nativeQuery(EntityManager em, String s) {
      System.out.printf("'%s'%n", s);
      Query query = em.createNativeQuery(s);
      List list = query.getResultList();
      for (Object o : list) {
          if (o instanceof Object[]) {
              System.out.println(Arrays.toString((Object[]) o));
          } else {
              System.out.println(o);
          }
      }
  }
}

Output

'SHOW TABLES'
[CUSTOMER, PUBLIC]
[CUSTOMER_PHONENUMBERS, PUBLIC]
'SHOW COLUMNS from Customer'
[ID, INTEGER(10), NO, PRI, NULL]
[NAME, VARCHAR(255), YES, , NULL]
'SHOW COLUMNS from Customer_PhoneNumbers'
[CUSTOMER_ID, INTEGER(10), NO, , NULL]
[PHONENUMBER, VARCHAR(255), YES, , NULL]
[TYPE, VARCHAR(255), YES, , NULL]

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.setPhoneNumbers(Arrays.asList(
              new PhoneNumber("111-111-1111", PhoneType.Cell),
              new PhoneNumber("222-222-2222", PhoneType.House)
      ));
      System.out.println(c1);

      Customer c2 = new Customer();
      c2.setName("Morgan Philips");
      c2.setPhoneNumbers(Arrays.asList(
              new PhoneNumber("333-333-3333", PhoneType.Work)
      ));
      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_PhoneNumbers");
  }

  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', phoneNumbers=[PhoneNumber{phoneNumber='111-111-1111', type=Cell}, PhoneNumber{phoneNumber='222-222-2222', type=House}]}
Customer{id=0, name='Morgan Philips', phoneNumbers=[PhoneNumber{phoneNumber='333-333-3333', type=Work}]}
-- Native queries --
'Select * from Customer'
[1, Lindsey Craft]
[2, Morgan Philips]
'Select * from Customer_PhoneNumbers'
[1, 111-111-1111, Cell]
[1, 222-222-2222, House]
[2, 333-333-3333, Work]
-- Loading Customer --
Customer{id=1, name='Lindsey Craft', phoneNumbers=[PhoneNumber{phoneNumber='111-111-1111', type=Cell}, PhoneNumber{phoneNumber='222-222-2222', type=House}]}
Customer{id=2, name='Morgan Philips', phoneNumbers=[PhoneNumber{phoneNumber='333-333-3333', type=Work}]}

Example Project

Dependencies and Technologies Used :

  • h2 1.4.193: H2 Database Engine.
  • hibernate-core 5.2.8.Final: The core O/RM functionality as provided by Hibernate.
  • JDK 1.8
  • Maven 3.3.9

Embedded Element Collection Example Select All Download
  • element-collection-with-embeddable
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF

See Also