0

I have set the property lazy="false" in my hbm file for the column Contact in the table "Employee"

The below query is executed to retrieve the Employee with Id 5:

Select e from Employee e where e.id = 5

On execution, a number of queries are being executed and I guess it is due to non Lazy Loading.

How to make sure that only one query is executed? Can we change the query keeping the property lazy as false? If not, what should be the change in the query to retrieve the record?

I'm using Spring+Hibernate

1 Answer 1

2

Based on your query, I'm going to assume you're using HQL for this. I'm also going to assume Contact is a reference.

select e from Employee e left join fetch e.Contact where e.Id = 5
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks. Yes im using HQL and Contact is a reference. Here we should set lazy="true" yea?
Setting lazy to true or false won't change this query getting the Contact automatically. You'll have to decide based on your requirements and performance if you want Contact to be default lazy and only eagerly load in this query or not.
so that means if I set lazy="false" and execute the above query, Contact will not be loaded? I thought setting lazy="false" will load the Contact always?
Maybe I didn't understand the original point. I thought you were looking for a way to keep Contact lazy but load it eagerly sometimes. Executing the above query will load Contact every time regardless of the lazy loading setting. Are you looking for something else?
Yea, it will change getting the employee from 2 queries to 1 query. This query does the same as setting the Contact to eagerly loading with join fetch. If you have multiple collection references you'll have to look out for cross joins, more info on that here
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.