Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Tuesday, July 27, 2010

Lazy Initilization in Hibernate

1)
when loading an object that has a collection, hibernate loads collection elements ONLY when actually you ask for them; so this improves performance.

2)
lazy initialization improves performance by delaying the fetch from the database until the returned object is actually queried for the data...it works similar to write behind caching on your hard disk ;-) .

3)

when you use hibernate
you can make lazy=true or lazy=false.


if
you mentioned lazy=true

and
you call from a company bean like

Collection x = company.getAllEmployees();

x will be empty;


if u mentioned lazy=false

x will contain the data as output of the getter funtion.


So if
you put lazy=true then you need to write HQL query getting these kind of collections. else if lazy = false u can get the collection at the time when u fetch company object.


So taking performance(time cost and memory cost) as criteria the its better to make lazy=true.