JPA - Embeddable Classes

[Updated: May 19, 2017, Created: May 19, 2017]
A quick overview of JPA embeddable classes
  • An entity may have references of other non-entity classes. Such non-entity classes are called embeddable classes.
  • All fields of an embeddable class are mapped to the owner entity table.
  • @Embeddable annotation is used on the non-entity class. This is the class which is to be embedded in an entity class.
  • @Embedded is used in the entity class. This annotation is placed on the field/property referring to the embeddable class.
  • Embedded objects belong strictly to their owning entity, and should not be shared across persistent entities.
  • Embeddable class and it's fields/properties can use all annotations used by an entity (e.g. @Column etc), except for the @Entity annotation.

Example

@Entity
public class EntityA {
  @Id
  @GeneratedValue
  private int id;

  @Embedded
  private ClassA classARef;
    .............
}
@Embeddable
public class ClassA {
  private String myStr;
  private int myInt;
    .............
}
public class ExampleMain {

      public static void main(String[] args) {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
      try {
          EntityManager em = emf.createEntityManager();
          nativeQuery(em, "SHOW TABLES");
          nativeQuery(em, "SHOW COLUMNS from EntityA");
          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'
[ENTITYA, PUBLIC]
'SHOW COLUMNS from EntityA'
[ID, INTEGER(10), NO, PRI, NULL]
[MYINT, INTEGER(10), NO, , NULL]
[MYSTR, VARCHAR(255), YES, , NULL]

H2 database SHOW statements

A quick overview of the example:

Persisting and loading data

public class ExampleMain2 {

  public static void main(String[] args) {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
      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();

      EntityA entityA = new EntityA();
      ClassA a = new ClassA();
      a.setMyInt(10);
      a.setMyStr("a test String");
      entityA.setClassARef(a);

      System.out.println(entityA);

      em.getTransaction().begin();
      em.persist(entityA);
      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 EntityA");
  }

  private static void loadEntity(EntityManagerFactory emf) {
      System.out.println("-- Loading EntityA --");
      EntityManager em = emf.createEntityManager();
      List<EntityA> entityAList = em.createQuery("Select t from EntityA t")
                                    .getResultList();
      entityAList.forEach(System.out::println);
      em.close();
  }
}

Output

-- Persisting entities --
EntityA{id=0, classARef=ClassA{myStr='a test String', myInt=10}}
-- native queries --
'Select * from EntityA'
[1, 10, a test String]
-- Loading EntityA --
EntityA{id=1, classARef=ClassA{myStr='a test String', myInt=10}}

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

Embeddable Classes Example Select All Download
  • embeddable-examples
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF

See Also