@OneToOne(fetch = FetchType.EAGER)
and @Fetch(FetchMode.JOIN)
are same as mentioned here you are eager loading Address
that means whenever Employee
is ask to fetch Address
will also be fetched ,as per this doucment
second select will only be executed when you access the association.
that means query for Address
will be executed only when you access it so you should use @Fetch(FetchMode.SELECT)
and @OneToOne(fetch = FetchType.LAZY)
.
Update:
From reference with @Swathi's post FetchMode
is always ignored by Hibernate when used Query
, it will use select for each individual entity, collection, or join load in that case. This means you have to create join your self
For lazy loading Hibernate will ignore FetchType
provided through annotation but if mapping is done through XML e.g.Employee.hbm.xml
with some thing like this
<one-to-one name="address" fetch="join" class="**PACKAGE**.Address"
constrained="true" ></one-to-one>
then Hibernate will respect lazy loading and only execute first select statement if lazy="false"
is set on one-to-one
then it will execute both select statements.