Employee: Most interesting class.
One employee can have many jobs. Hence a map of ID-vs-Job.
A list could also have sufficed here but we wanted to show a slightly complex case.
One employee can also have one manager per company.
Hence a map of Manager-vs-Company.
package com.prismoskills.hibernate.hierarchy.pojos.fields;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "COMPANY")
public class Company extends BaseEntity
{
String name;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "EMPLOYEE_ID")
Employee engineer;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "MANAGER_ID", nullable = false)
Manager manager;
public Company()
{
super();
}
public Company(String name)
{
super();
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Employee getEngineer()
{
return engineer;
}
public void setEngineer(Employee engineer)
{
this.engineer = engineer;
}
public Manager getManager()
{
return manager;
}
public void setManager(Manager manager)
{
this.manager = manager;
}
@Override
public String toString()
{
return "Company [name=" + name + "]";
}
}
Output
insert into EMPLOYEE
(name, id) values (?, ?)
insert into MANAGER
(name, id) values (?, ?)
insert into COMPANY
(EMPLOYEE_ID, MANAGER_ID, name, id) values (?, ?, ?, ?)
insert into JOB
(designation, EMPLOYEE_ID, id) values (?, ?, ?)
// Finished save. Now reading.
SELECT
emp.id as id1_3_0,
emp.name as name2_3_0,
cmap.EMPLOYEE_ID as EMPLOYEE_ID3_3_1,
cmap.id as id1_1_1,
cmap.id as formula0_1,
cmap.id as id1_1_2,
cmap.EMPLOYEE_ID as EMPLOYEE_ID3_1_2,
cmap.MANAGER_ID as MANAGER_ID4_1_2,
cmap.name as name2_1_2,
mgr.id as id1_5_3,
mgr.name as name2_5_3,
jobs.EMPLOYEE_ID as EMPLOYEE_ID3_3_4,
jobs.id as id1_4_4,
jobs.id as formula1_4,
jobs.id as id1_4_5,
jobs.designation as designation2_4_5,
jobs.EMPLOYEE_ID as EMPLOYEE_ID3_4_5
from
EMPLOYEE emp
left outer join
COMPANY cmap
on emp.id = cmap.EMPLOYEE_ID
left outer join
MANAGER mgr
on cmap.MANAGER_ID = mgr.id
left outer join
JOB jobs
on emp.id = jobs.EMPLOYEE_ID
where
emp.id = ?
Delete from COMPANY where id=?
Delete from MANAGER where id=?
Delete from JOB where id=?
Delete from EMPLOYEE where id=?
Insertion Order
First of all, Hibernate inserts into Employee and Manager
tables because they do not have any foreign keys.
Then it inserts data into Company and Job tables because now
it has foreign keys for those tables.
Select Query
Observe the select query. For efficiency, Hibernate
loads all the data in one go. This is because of the
EAGER annotation in the above POJO classes.
If the EAGER annotation was not there, then Hibernate
would do lazy-loading by default and only the foreign-keys
would be loaded without joins. Something like this:
select e.id, e.name
from EMPLOYEE e
where e.id=?
Delete Queries
Deletion order is somewhat opposite to the insertion order.
This is again to mitigate the foreign-key constraints.
Hibernate first deletes the entry which has a foreign-key field.
If it were to delete the child table first, then database would
given an error for invalid foreign key in the parent table.
Got a thought to share or found a bug in the code? We'd love to hear from you: