I have a one2one relation between Student and Address. I want the firstName and lastName fields of Student to be lazy loaded. Also I want lazy for the address field.
These are my entity clasess:
Code:
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(mappedBy = "student", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@LazyToOne(LazyToOneOption.NO_PROXY)
private Address address;
@Basic(fetch = FetchType.LAZY)
@Column(name = "first_name")
private String firstName;
@Basic(fetch = FetchType.LAZY)
@Column(name = "last_name")
private String lastName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Student [id=" + id + ", address=" + address + ", firstName="
+ firstName + ", lastName=" + lastName + "]";
}
}
The Address class:
Code:
@Entity
@Table(name = "addresses")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne
@JoinColumn(name = "s_id")
private Student student;
@Column
private String street;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [id=" + id + ", street=" + street + "]";
}
}
My test method looks like this ( the Java 8 lambda is just creating an entitymanager in back and executes all in transaction ):
Code:
@Test
public void dummyTest() {
JPA_UTILS.runInTransaction(e -> {
Student s = e.find(Student.class, 150L);
System.out.println("----------++++++++++++++-----------");
s.getFirstName();
System.out.println("----------++++++++++++++-----------");
});
}
So here I am loading an existing student from the database, then fetch the lazy property firstName ( mapped to first_name column ). The problem is that Hibernate doesn't load only firstName but also lastName and address fields:
Code:
just.hibernate.one2one.TestApp > dummyTest STANDARD_OUT
Hibernate:
select
student0_.id as id1_1_0_
from
students student0_
where
student0_.id=?
----------++++++++++++++-----------
Hibernate:
/* sequential select
just.hibernate.one2one.Student */ select
student_.first_name as first_na2_1_,
student_.last_name as last_nam3_1_
from
students student_
where
student_.id=?
Hibernate:
/* load just.hibernate.one2one.Address */ select
address0_.id as id1_0_1_,
address0_.street as street2_0_1_,
address0_.s_id as s_id3_0_1_,
student1_.id as id1_1_0_
from
addresses address0_
left outer join
students student1_
on address0_.s_id=student1_.id
where
address0_.s_id=?
----------++++++++++++++-----------
I don't want this behavior. I would have expected to query just the firstName field. Not lastName and by any means the address relation.
Is it a bug or just the intended behavior of Hibernate/JPA?
Thanks