Assume there are two entities with OneToOne relation:
Code:
@Entity
public class A {
private long code;
private B b;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public long getCode() {
return code;
}
public viod setCode(long code) {
this.code = code;
}
@OneToOne(fetch=FetchType.LAZY, mappedBy="a", cascade={CascadeType.ALL})
public B getB() {
return b;
}
public void setB(B b) {
this.b = b
}
// additional setters and getters
}
@Entity
public class B {
private long code;
private A a;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public long getCode() {
return code;
}
public viod setCode(long code) {
this.code = code;
}
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="acode", nullable=false)
public A getA() {
return a;
}
public void setA(A a) {
this.a = a
}
// additional setters and getters
}
The problem is that when I am fetching A object, the B object is loaded as well even though I didn't call getB() method. Why hibernate performs this redundant query?
Is this a regular behaviour with bidirectional relations?