This example will show how to map a java.util.Map having entity keys and entity values.
As we have already learnt in previous tutorials, to map entity values, we need to use @OneToMany/@ManyToMany and to map entity keys, we need to use @ElementCollection. This example is simply a combination of the two cases.
@OneToMany Example
@Entity
public class Employee {
@Id
@GeneratedValue
private long id;
private String name;
@ElementCollection
@OneToMany
private Map<AssignInfo, Task> taskMap;
.............
}
@Entity
public class AssignInfo {
@Id
@GeneratedValue
private long id;
private Date startDate;
private Date endDate;
.............
}
@Entity
public class Task {
@Id
@GeneratedValue
private long id;
private String name;
private String description;
.............
}
Note that using both @OneToMany and @ElementCollection together is redundant. Hibernate can recognise the intended mapping, if we use just one of them.
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 EMPLOYEE");
nativeQuery(em, "SHOW COLUMNS from TASK");
nativeQuery(em, "SHOW COLUMNS from EMPLOYEE_TASK");
nativeQuery(em, "SHOW COLUMNS from ASSIGNINFO");
} finally {
emf.close();
}
}
.............
} Output'SHOW TABLES' [ASSIGNINFO, PUBLIC] [EMPLOYEE, PUBLIC] [EMPLOYEE_TASK, PUBLIC] [TASK, PUBLIC] 'SHOW COLUMNS from EMPLOYEE' [ID, BIGINT(19), NO, PRI, NULL] [NAME, VARCHAR(255), YES, , NULL] 'SHOW COLUMNS from TASK' [ID, BIGINT(19), NO, PRI, NULL] [DESCRIPTION, VARCHAR(255), YES, , NULL] [NAME, VARCHAR(255), YES, , NULL] 'SHOW COLUMNS from EMPLOYEE_TASK' [EMPLOYEE_ID, BIGINT(19), NO, PRI, NULL] [TASKMAP_ID, BIGINT(19), NO, UNI, NULL] [TASKMAP_KEY, BIGINT(19), NO, PRI, NULL] 'SHOW COLUMNS from ASSIGNINFO' [ID, BIGINT(19), NO, PRI, NULL] [ENDDATE, TIMESTAMP(23), YES, , NULL] [STARTDATE, TIMESTAMP(23), YES, , NULL]
H2 database SHOW statements
A quick overview of the mapping:
In the join table (EMPLOYEE_TASK) the foreign key pointing to the map value's primary key (TASK table in above example) is unique. That's because it's a OneToMany relation, which does not allow to share the same target entity. If we use @ManyToMany, then there won't be any unique constraint.
Complete Example
Persisting and loading data
public class ExampleMain2 {
public static void main(String[] args) throws Exception {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("example-unit");
try {
persistEntity(emf);
runNativeQueries(emf);
loadEntity(emf);
} finally {
emf.close();
}
}
private static void persistEntity(EntityManagerFactory emf) throws Exception {
System.out.println("-- Persisting entities --");
EntityManager em = emf.createEntityManager();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yy");
Employee e1 = new Employee();
e1.setName("employee name 1");
Task task1 = new Task();
task1.setName("task 1");
task1.setDescription("task 1 desc");
AssignInfo assignInfo1 = new AssignInfo();
assignInfo1.setStartDate(sdf.parse("15-01-17"));
assignInfo1.setEndDate(sdf.parse("20-01-17"));
e1.addTask( assignInfo1, task1);
Task task2 = new Task();
task2.setName("task 2");
task2.setDescription("task 2 desc");
AssignInfo assignInfo2 = new AssignInfo();
assignInfo2.setStartDate(sdf.parse("02-01-17"));
assignInfo2.setEndDate(sdf.parse("11-02-17"));
e1.addTask(assignInfo2, task2);
System.out.println(e1);
Employee e2 = new Employee();
e2.setName("employee name 2");
Task task3 = new Task();
task3.setName("task 3");
task3.setDescription("task 3 desc");
AssignInfo assignInfo3 = new AssignInfo();
assignInfo3.setStartDate(sdf.parse("9-02-17"));
assignInfo3.setEndDate(sdf.parse("19-03-17"));
e2.addTask(assignInfo3, task3);
System.out.println(e2);
em.getTransaction().begin();
em.persist(task1);
em.persist(task2);
em.persist(task3);
em.persist(assignInfo1);
em.persist(assignInfo2);
em.persist(assignInfo3);
em.persist(e1);
em.persist(e2);
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 Employee");
ExampleMain.nativeQuery(em, "Select * from Task");
ExampleMain.nativeQuery(em, "Select * from Employee_Task");
ExampleMain.nativeQuery(em, "Select * from AssignInfo");
}
private static void loadEntity(EntityManagerFactory emf) {
System.out.println("-- Loading entities --");
EntityManager em = emf.createEntityManager();
List<Employee> entityAList = em.createQuery("Select t from Employee t")
.getResultList();
entityAList.forEach(System.out::println);
em.close();
}
} Output-- Persisting entities -- Employee{id=0, name='employee name 1', taskMap={AssignInfo{id=0, startDate=Thu Mar 01 00:00:00 CST 2018, endDate=Wed Aug 01 00:00:00 CDT 2018}=Task{id=0, name='task 2', description='task 2 desc'}}} Employee{id=0, name='employee name 2', taskMap={AssignInfo{id=0, startDate=Sat Sep 02 00:00:00 CDT 2017, endDate=Tue Jul 03 00:00:00 CDT 2018}=Task{id=0, name='task 3', description='task 3 desc'}}} -- Native queries -- 'Select * from Employee' [7, employee name 1] [8, employee name 2] 'Select * from Task' [1, task 1 desc, task 1] [2, task 2 desc, task 2] [3, task 3 desc, task 3] 'Select * from Employee_Task' [7, 2, 4] [8, 3, 6] 'Select * from AssignInfo' [4, 2018-08-01 00:00:00.0, 2018-03-01 00:00:00.0] [5, 2017-11-02 00:00:00.0, 2017-02-01 00:00:00.0] [6, 2018-07-03 00:00:00.0, 2017-09-02 00:00:00.0] -- Loading entities -- Employee{id=7, name='employee name 1', taskMap={AssignInfo{id=4, startDate=2018-03-01 00:00:00.0, endDate=2018-08-01 00:00:00.0}=Task{id=2, name='task 2', description='task 2 desc'}}} Employee{id=8, name='employee name 2', taskMap={AssignInfo{id=6, startDate=2017-09-02 00:00:00.0, endDate=2018-07-03 00:00:00.0}=Task{id=3, name='task 3', description='task 3 desc'}}}
Example ProjectDependencies and Technologies Used: - h2 1.4.195: 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
|
|