With eager you have following behavior:
Code:
@Entity A {
...
@javax.persistence.ManyToOne(fetch= FetchType.EAGER)
public D assSingleD = null;
}
Code:
a = (A) s.get(A.class, id_a); // a and d are read together with a join query, see below
Code:
select
a0_.oid as oid2_1_,
a0_.assSingleD_oid as assSingleD4_2_1_,
a0_.name as name2_1_,
a0_.version as version2_1_,
d1_.oid as oid5_0_,
d1_.assA_oid as assA3_5_0_,
d1_.version as version5_0_
from
A a0_
left outer join
D d1_
on a0_.assSingleD_oid=d1_.oid
where
a0_.oid=1
Code:
D d = a.getAssSingleD(); // not database hit, d is already loaded
System.out.println(d.getVersion());
With lazy indeed you have following behavior:
Code:
@Entity A {
...
@javax.persistence.ManyToOne(fetch= FetchType.LAZY)
public D assSingleD = null;
}
Code:
a = (A) s.get(A.class, id_a); // only a is selected from database, see below
Code:
select
a0_.oid as oid2_0_,
a0_.assSingleD_oid as assSingleD4_2_0_,
a0_.name as name2_0_,
a0_.version as version2_0_
from
A a0_
where
a0_.oid=1
Code:
D d = a.getAssSingleD(); // not database hit, d is a hibernate proxy
System.out.println(d.getVersion()); // only here where I access informations of d, the query on d is executed, see below
Code:
select
d0_.oid as oid5_0_,
d0_.assA_oid as assA3_5_0_,
d0_.version as version5_0_
from
D d0_
where
d0_.oid=1
I Hope this is clear someway.