Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between lazy="true" and fetch="select" in hibernate?

The lazy=true attribute is enable lazy loading of the parent and child collections and same thing fetch="select" attribute. Is there any difference between lazy="true" and fetch="select" in hibernate?.

like image 289
user1127214 Avatar asked Mar 21 '12 13:03

user1127214


People also ask

What is difference between lazy true and lazy false in Hibernate?

To use lazy collection, you may optionally use lazy="true" attribute in your collection. It is by default true, so you don't need to do this. If you set it to false, all the child objects will be loaded initially which will decrease performance in case of big data.

What is the difference between fetch type eager and lazy?

FetchType. LAZY = This does not load the relationships unless you invoke it via the getter method. FetchType. EAGER = This loads all the relationships.

What is fetch select in Hibernate?

The Hibernate FetchMode. SELECT generates a separate query for each Order that needs to be loaded. In our example, that gives one query to load the Customers and five additional queries to load the orders collection. This is known as the n + 1 select problem. Executing one query will trigger n additional queries.

What is lazy fetching in Hibernate?

Lazy loading in Hibernate means fetching and loading the data, only when it is needed, from a persistent storage like a database. Lazy loading improves the performance of data fetching and significantly reduces the memory footprint.


1 Answers

Yes.

The lazy attribute tells hibernate when to get the children.

The fetch attribute tells hibernate how to get the children.

When you say

The lazy=true attribute is enable lazy loading of the parent and child collections and same thing fetch="select" attribute

that is flat out incorrect. The select fetch strategy is NOT the same thing as turning lazy loading off. In fact, from the documentation

Select fetching: a second SELECT is used to retrieve the associated entity or collection. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you access the association.

like image 124
hvgotcodes Avatar answered Oct 21 '22 00:10

hvgotcodes