0

I have employee and employee detail classes mapped (bi directional) using primary key join column (employee_id)

@Entity
@Table(name="employee")
public class Employee {

@Id
@GeneratedValue
@Column(name="employee_id")
private Long employeeId;

@Column(name="firstname")
private String firstname;

@Column(name="lastname")
private String lastname;

@Column(name="birth_date")
@Temporal(value = TemporalType.DATE )
private Date birthDate;

@Column(name="cell_phone")
private String cellphone;

@OneToOne(mappedBy="empl", cascade=CascadeType.ALL)
private EmployeeDetail employeeDetail;

...
}



@Table(name="employeedetail")
public class EmployeeDetail {


@Id
@Column(name="employee_id", unique=true, nullable=false)
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="empl"))
private Long employeeId;

@Column(name="street")
private String street;

@Column(name="city")
private String city;

@Column(name="state")
private String state;

@Column(name="country")
private String country;

@OneToOne
@PrimaryKeyJoinColumn
private Employee empl;
....................
}

Can someone please clarify why when executing the following query session.createQuery("from Employee").list();

A) the join queries like 2),3) and 4) get executed to fetch data from employeedetail? why not directly fetch from employeedetail table using the employeeid value in employee object without a join?

1) Hibernate: select employee0_.employee_id as employee1_0_, employee0_.birth_date as birth_da2_0_, employee0_.cell_phone as cell_pho3_0_, employee0_.firstname as firstnam4_0_, employee0_.lastname as lastname5_0_ from employee employee0_ 

2) Hibernate: select employeede0_.employee_id as employee1_1_0_, employeede0_.city as city2_1_0_, employeede0_.country as country3_1_0_, employeede0_.state as state4_1_0_, employeede0_.street as street5_1_0_, employee1_.employee_id as employee1_0_1_, employee1_.birth_date as birth_da2_0_1_, employee1_.cell_phone as cell_pho3_0_1_, employee1_.firstname as firstnam4_0_1_, employee1_.lastname as lastname5_0_1_ from employeedetail employeede0_ left outer join employee employee1_ on employeede0_.employee_id=employee1_.employee_id where employeede0_.employee_id=? 
3) Hibernate: select employeede0_.employee_id as employee1_1_0_, employeede0_.city as city2_1_0_, employeede0_.country as country3_1_0_, employeede0_.state as state4_1_0_, employeede0_.street as street5_1_0_, employee1_.employee_id as employee1_0_1_, employee1_.birth_date as birth_da2_0_1_, employee1_.cell_phone as cell_pho3_0_1_, employee1_.firstname as firstnam4_0_1_, employee1_.lastname as lastname5_0_1_ from employeedetail employeede0_ left outer join employee employee1_ on employeede0_.employee_id=employee1_.employee_id where employeede0_.employee_id=? 
4) Hibernate: select employeede0_.employee_id as employee1_1_0_, employeede0_.city as city2_1_0_, employeede0_.country as country3_1_0_, employeede0_.state as state4_1_0_, employeede0_.street as street5_1_0_, employee1_.employee_id as employee1_0_1_, employee1_.birth_date as birth_da2_0_1_, employee1_.cell_phone as cell_pho3_0_1_, employee1_.firstname as firstnam4_0_1_, employee1_.lastname as lastname5_0_1_ from employeedetail employeede0_ left outer join employee employee1_ on employeede0_.employee_id=employee1_.employee_id where employeede0_.employee_id=? 

B) also, for the query session.createQuery("from EmployeeDetail").list();

why employee info is fetched as per below select queries instead of being proxied? 


Hibernate: select employeede0_.employee_id as employee1_1_, employeede0_.city as city2_1_, employeede0_.country as country3_1_, employeede0_.state as state4_1_, employeede0_.street as street5_1_ from employeedetail employeede0_ 

Hibernate: select employee0_.employee_id as employee1_0_0_, employee0_.birth_date as birth_da2_0_0_, employee0_.cell_phone as cell_pho3_0_0_, employee0_.firstname as firstnam4_0_0_, employee0_.lastname as lastname5_0_0_ from employee employee0_ where employee0_.employee_id=? 
Hibernate: select employee0_.employee_id as employee1_0_0_, employee0_.birth_date as birth_da2_0_0_, employee0_.cell_phone as cell_pho3_0_0_, employee0_.firstname as firstnam4_0_0_, employee0_.lastname as lastname5_0_0_ from employee employee0_ where employee0_.employee_id=? 
Hibernate: select employee0_.employee_id as employee1_0_0_, employee0_.birth_date as birth_da2_0_0_, employee0_.cell_phone as cell_pho3_0_0_, employee0_.firstname as firstnam4_0_0_, employee0_.lastname as last`enter code here`name5_0_0_ from employee employee0_ where employee0_.employee_id=?
Neil Stockton
  • 10,531
  • 3
  • 28
  • 27
user2221654
  • 271
  • 1
  • 5
  • 17

2 Answers2

0

A) It is a little bit weird, and Hibernate could do it better. However, this is due to the fact that Employee is loaded eagerly in EmployeeDetail, and Hibernate wants to avoid additional queries to load the corresponding employee for each EmployeeDetail. It could do it better because the corresponding employees are already loaded with the first query.

This should disappear if you apply my advice in B).

B) By default, one-to-one associations are eager in JPA. To make it lazy:

@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private Employee empl;

However, keep in mind the behavior of one-to-one associations described here. If Hibernate cannot determine whether the proxied object in one-to-one relation exists or is null, then additional query will be executed anyway to check this.

To overcome this, if empl is mandatory for each EmployeeDetail (and it is in your use case, an employee detail cannot exist without the employee), then indicate it in the association mapping:

@OneToOne(fetch = FetchType.LAZY, optional = false)
@PrimaryKeyJoinColumn
private Employee empl;

This way Hibernate will make a proxy containing only Employee id when loading EmployeeDetail entity instances without querying employee table at all.

Community
  • 1
  • 1
Dragan Bozanovic
  • 21,631
  • 4
  • 36
  • 100
  • If I want to use `OneToOne` with cascade delete (when `Employee` deleted, `EmployeeDetails` also deleted), how it would be with `PrimaryKeyJoinColumn`? – Woland Apr 07 '19 at 13:21
0

Can someone please clarify why when executing the following query session.createQuery("from Employee").list();

This should be happening because your employee table must have 3 entries. Since it is a one-to-one mapping, for each row returned from employee it will fire a query to fetch its employeedetail. So the three queries.

B) also, for the query session.createQuery("from EmployeeDetail").list();

Proxies are only created for lazily loaded entities.

Johnson Abraham
  • 741
  • 4
  • 12