Close

JPA - Derived Composite key (The parent entity uses @IdClass)

[Last Updated: Aug 17, 2017]

The derived identities may be captured by means of of composite primary. In this example, we will demonstrate how the @ManyToOne target entity will use @IdClass to define a composite primary key.

Example

@Entity
@IdClass(CompositeTaskId.class)
public class Task {
  @Id
  private long taskId;

  @Id
  @ManyToOne
  @JoinColumns({
          @JoinColumn(name = "FK_firstName", referencedColumnName = "firstName"),
          @JoinColumn(name = "FK_lastName", referencedColumnName = "lastName")
  })
  private Employee employee;

  private String taskName;
  private Date date;

  public Task() {
  }

  public Task(Employee employee, long taskId) {
      this.employee = employee;
      this.taskId = taskId;
  }
    .............
}

Note that using @JoinColumns in above class, is optional. If we don't use it then JPA uses the default naming strategy for foreign key columns.


Following class which is the target of @ManyToOne relationship (also called 'parent' from database perspective as it will correspond to a foreign key referenced table), will define a composite primary key using @IdClass:

@Entity
@IdClass(EmployeeId.class)
public class Employee {
  @Id
  private String firstName;
  @Id
  private String lastName;
  private String dept;

  public Employee() {
  }

  public Employee(String firstName, String lastName, String dept) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.dept = dept;
  }
    .............
}
public class EmployeeId implements Serializable{
  private String firstName;
  private String lastName;

  public EmployeeId() {
  }

  public EmployeeId(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
  }
    .............
}
public class CompositeTaskId implements Serializable {
  private EmployeeId employee;
  private long taskId;

  public CompositeTaskId() {
  }

  public CompositeTaskId(EmployeeId employee, long taskId) {
      this.employee = employee;
      this.taskId = taskId;
  }
    .............
}

Main class showing table mappings

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 TASK");
          nativeQuery(em, "SHOW COLUMNS from EMPLOYEE");
      } 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'
[EMPLOYEE, PUBLIC]
[TASK, PUBLIC]
'SHOW COLUMNS from TASK'
[TASKID, BIGINT(19), NO, PRI, NULL]
[DATE, TIMESTAMP(23), YES, , NULL]
[TASKNAME, VARCHAR(255), YES, , NULL]
[FK_FIRSTNAME, VARCHAR(255), NO, PRI, NULL]
[FK_LASTNAME, VARCHAR(255), NO, PRI, NULL]
'SHOW COLUMNS from EMPLOYEE'
[FIRSTNAME, VARCHAR(255), NO, PRI, NULL]
[LASTNAME, VARCHAR(255), NO, PRI, NULL]
[DEPT, VARCHAR(255), YES, , NULL]

H2 database SHOW statements

Persisting and loading data

public class ExampleMain2 {

  public static void main(String[] args) throws Exception {
      EntityManagerFactory emf =
              Persistence.createEntityManagerFactory("example-unit");
      try {
          persistEntity(emf);
          runNativeQuery(emf);
          findEntityById(emf);
      } finally {
          emf.close();
      }
  }

  private static void persistEntity(EntityManagerFactory emf) throws Exception {
      System.out.println("-- Persisting entity --");
      EntityManager em = emf.createEntityManager();

      Employee e = new Employee( "Darion"," Schaden", "IT");

      Task task1 = new Task(e, 100L);
      task1.setTaskName("Coding");
      task1.setDate(new Date());

      Task task2 = new Task(e, 200L);
      task2.setTaskName("Refactoring");
      task2.setDate(new Date());

      em.getTransaction().begin();
      em.persist(e);
      em.persist(task1);
      em.persist(task2);
      em.getTransaction().commit();
      em.close();
  }

  private static void runNativeQuery(EntityManagerFactory emf) {
      System.out.println("-- Native query --");
      EntityManager em = emf.createEntityManager();
      ExampleMain.nativeQuery(em, "Select * from EMPLOYEE");
      ExampleMain.nativeQuery(em, "Select * from Task");
  }

  private static void findEntityById(EntityManagerFactory emf) {
      System.out.println("-- Finding entity --");
      EntityManager em = emf.createEntityManager();
      EmployeeId employeeId = new EmployeeId("Darion", " Schaden");
      CompositeTaskId taskId = new CompositeTaskId(employeeId, 100);
      Task task = em.find(Task.class, taskId);
      System.out.println(task);
      em.close();
  }
}

Output

-- Persisting entity --
-- Native query --
'Select * from EMPLOYEE'
[Darion, Schaden, IT]
'Select * from Task'
[100, 2017-08-17 14:27:17.587, Coding, Darion, Schaden]
[200, 2017-08-17 14:27:17.587, Refactoring, Darion, Schaden]
-- Finding entity --
Task{taskId=100, employee=Employee{firstName='Darion', lastName=' Schaden', dept='IT'}, taskName='Coding', date=2017-08-17 14:27:17.587}

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

@ManyToOne Composite Derived Key with @IdClass Example Select All Download
  • composite-derived-key
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Employee.java
          • resources
            • META-INF

    See Also