-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: one to one mapping using primary key join column question
PostPosted: Sun Jun 28, 2015 8:36 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2010 3:35 pm
Posts: 37
I have employee and employeedetail classes mapped using primary key join column (employee_id) (bi directional)





@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 lastname5_0_0_ from employee employee0_ where employee0_.employee_id=?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.