This example shows how to define a composite key by using @IdClass having an identity which is derived from a @ManyToOne relationship.
This example is very similar to our example of using @IdClass with @OneToOne, but with one difference; we will use @ManyToOne instead of @OneToOne. The ManyToOne relationship allows us to persist multiple target entity instances referencing the same source entity.
Check out our ManyToOne tutorial as well, if not already familiar with it.
Example
@Entity
@IdClass(CompositeTaskId.class)
public class Task {
@Id
private long taskId;
@Id
@ManyToOne
private Employee employee;
private String taskName;
private Date date;
public Task() {
}
public Task(Employee employee, long taskId) {
this.employee = employee;
this.taskId = taskId;
}
.............
}
@Entity
public class Employee {
@Id
private long employeeId;
private String name;
private String dept;
public Employee() {
}
public Employee(long employeeId, String name, String dept) {
this.employeeId = employeeId;
this.name = name;
this.dept = dept;
}
.............
}
public class CompositeTaskId implements Serializable{
private long employee;
private long taskId;
public CompositeTaskId() {
}
public CompositeTaskId(long employeeId, long taskId) {
this.employee = employeeId;
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] [EMPLOYEE_EMPLOYEEID, BIGINT(19), NO, PRI, NULL] 'SHOW COLUMNS from EMPLOYEE' [EMPLOYEEID, BIGINT(19), NO, PRI, NULL] [DEPT, VARCHAR(255), YES, , NULL] [NAME, 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(1L, "Mike", "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();
CompositeTaskId taskId = new CompositeTaskId(1, 100);
Task task = em.find(Task.class, taskId);
System.out.println(task);
em.close();
}
} Output-- Persisting entity -- -- Native query -- 'Select * from EMPLOYEE' [1, IT, Mike] 'Select * from Task' [100, 2017-08-08 15:08:28.556, Coding, 1] [200, 2017-08-08 15:08:28.556, Refactoring, 1] -- Finding entity -- Task{taskId=100, employee=Employee{employeeId=1, name='Mike', dept='IT'}, taskName='Coding', date=2017-08-08 15:08:28.556}
Example ProjectDependencies and Technologies Used: - h2 1.4.196: 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
|
|