Map
is used when we want to persist a collection of key/value pairs where the key is always unique. Some common implementations of java.util.Map
are java.util.HashMap
, java.util.LinkedHashMap
, and so on. For this recipe, we will use java.util.HashMap
.
Now, let's assume that we have a scenario where we are going to implement Map<String, String>
; here, the String
key is the e-mail address label, and the value String
is the e-mail address. For example, we will try to construct a data structure similar to <"Personal e-mail"
, "[email protected]">
, <"Business e-mail"
, "[email protected]">
. This means that we will create an alias of the actual e-mail address so that we can easily get the e-mail address using the alias and can document it in a more readable form. This type of implementation depends on the custom requirement; here, we can easily get a business e-mail using the Business email
key.
Use the following code to create the required tables and classes.
Use the following script to create the tables if you are not using hbm2dll=create|update
. This script is for the tables that are generated by hibernate:
Use the following code to create the email
table:
CREATE TABLE `email` ( `Employee_id` BIGINT(20) NOT NULL, `emails` VARCHAR(255) DEFAULT NULL, `emails_KEY` VARCHAR(255) NOT NULL DEFAULT '', PRIMARY KEY (`Employee_id`,`emails_KEY`), KEY `FK5C24B9C38F47B40` (`Employee_id`), CONSTRAINT `FK5C24B9C38F47B40` FOREIGN KEY (`Employee_id`) REFERENCES `employee` (`id`) );
Use the following code to create the employee
table:
CREATE TABLE `employee` ( `id` BIGINT(20) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) DEFAULT NULL, PRIMARY KEY (`id`) );
@Entity @Table(name = "employee") public class Employee { @Id @GeneratedValue @Column(name = "id") private long id; @Column(name = "name") private String name; @ElementCollection @CollectionTable(name = "email") private Map<String, String> emails; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Map<String, String> getEmails() { return emails; } public void setEmails(Map<String, String> emails) { this.emails = emails; } @Override public String toString() { return "Employee" + " Id: " + this.id + " Name: " + this.name + " Emails: " + this.emails; } }
Here, we will consider how to work with Map
and its manipulation operations, such as inserting, retrieving, deleting, and updating.
Here, we will create one employee
record with two e-mail addresses:
Employee employee = new Employee(); employee.setName("yogesh"); Map<String, String> emails = new HashMap<String, String>(); emails.put("Business email", "[email protected]"); emails.put("Personal email", "[email protected]"); employee.setEmails(emails); session.getTransaction().begin(); session.save(employee); session.getTransaction().commit();
Hibernate: insert into employee (name) values (?) Hibernate: insert into email (Employee_id, emails_KEY, emails) values (?,?,?) Hibernate: insert into email (Employee_id, emails_KEY, emails) values (?,?,?)
When the code is executed, it inserts one record into the employee
table and two records into the email
table and also sets a primary key value for the employee
record in each record of the email
table as a reference.
Here, we know that our record is inserted with id
1
. So, we will try to get only that record and understand how Map
works in our case.
Employee employee = (Employee) session.get(Employee.class, 1l); System.out.println(employee.toString()); System.out.println("Business email: " + employee.getEmails().get("Business email"));
Hibernate: select employee0_.id as id0_0_, employee0_.name as name0_0_ from employee employee0_ where employee0_.id=? Hibernate: select emails0_.Employee_id as Employee1_0_0_, emails0_.emails as emails0_, emails0_.emails_KEY as emails3_0_ from email emails0_ where emails0_.Employee_id=? Employee Id: 1 Name: yogesh Emails: {Personal [email protected], Business [email protected]} Business email: [email protected]
Here, we can easily get a business e-mail address using the Business email
key from the map of e-mail addresses. This is just a simple scenario created to demonstrate how to persist Map
in hibernate.
Here, we will try to add one more e-mail address to Employee#1
:
Employee employee = (Employee) session.get(Employee.class, 1l); Map<String, String> emails = employee.getEmails(); emails.put("Personal email 1", "[email protected]"); session.getTransaction().begin(); session.saveOrUpdate(employee); session.getTransaction().commit(); System.out.println(employee.toString());
Hibernate: select employee0_.id as id0_0_, employee0_.name as name0_0_ from employee employee0_ where employee0_.id=? Hibernate: select emails0_.Employee_id as Employee1_0_0_, emails0_.emails as emails0_, emails0_.emails_KEY as emails3_0_ from email emails0_ where emails0_.Employee_id=? Hibernate: insert into email (Employee_id, emails_KEY, emails) values (?, ?, ?) Employee Id: 2 Name: yogesh Emails: {Personal email 1= [email protected], Personal [email protected], Business [email protected]}
Here, we added a new e-mail address with the Personal email 1
key and the value is [email protected]
.