Close

JPA - Embeddable class within another embeddable class Example

[Updated: May 31, 2017, Created: May 19, 2017]

An embeddable class may include another embeddable class. Let's see that with an example.

Example

@Embeddable
public class ClassA {
  private String myStr;
  private int myInt;
    .............
}
@Embeddable
public class ClassB {
  private String myStrB;
  @Embedded
  private ClassA classARef;
    .............
}
@Entity
public class MyEntity {
  @Id
  @GeneratedValue
  private int id;

  @Embedded
  private ClassB classBRef;
    .............
}
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 MyEntity");
          emf.close();
      } finally {
          emf.close();
      }
  }

  public static void nativeQuery(EntityManager em, String s) {
      System.out.printf("-----------------------------%n'%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'
[MYENTITY, PUBLIC]
-----------------------------
'SHOW COLUMNS from MyEntity'
[ID, INTEGER(10), NO, PRI, NULL]
[MYINT, INTEGER(10), NO, , NULL]
[MYSTR, VARCHAR(255), YES, , NULL]
[MYSTRB, VARCHAR(255), YES, , NULL]

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();

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

      ClassB b = new ClassB();
      b.setMyStrB("a test string in ClassB");
      b.setClassARef(a);

      MyEntity entityB = new MyEntity();
      entityB.setClassBRef(b);

      System.out.println(entityB);

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

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

Output

-- Persisting entities --
EntityB{id=0, classBRef=ClassB{myStrB='a test string in ClassB', classARef=ClassA{myStr='a test String', myInt=10}}}
-- native queries --
-----------------------------
'Select * from MyEntity'
[1, 10, a test String, a test string in ClassB]
-- Loading MyEntity --
EntityB{id=1, classBRef=ClassB{myStrB='a test string in ClassB', 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.
    Implements javax.persistence:javax.persistence-api version 2.1
  • JDK 1.8
  • Maven 3.3.9

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

    See Also