Javatpoint Logo

lazy vs eager loading

By: krupah*** On: Thu Oct 08 11:47:54 IST 2015     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
Dear ,

Can anyone please explain me lazy loading and Eager loading with examples !?

Thanks in advance
Up0Down

 
It is very simple..

Simply means,
lazy - Not Always(When ever you want that time you will get)
eager - Always(If you want or don't want is not a problem, always you will get)

if you put (fetch = FetchType.LAZY) that means
Whenever you trying to get the particular data then, that particular time only it will carry data for you. If you don't try to call then it won't get data.


But 'eager' always get the data while initialized.
if you need that data or no need that data, it will definitely carry all data for you where you are mentioned as 'eager'.


Example:

...
@Entity
@Table(name = "stock", catalog = "mkyong")
public class Stock implements Serializable{
...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "stock")
@Cascade(CascadeType.ALL)
@Fetch(FetchMode.SELECT)
@BatchSize(size = 10)
public Set<StockDailyRecord> getStockDailyRecords() {
return this.stockDailyRecords;
}
...
}


in these example (fetch = FetchType.LAZY) works like - when you trying to get that method that time only It will return the child record for you from the db.
if you put it as (fetch = FetchType.EAGER) - Then it will always return child record from the db.
Image Created0Down

By: [email protected] On: Fri Oct 09 11:56:27 IST 2015 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No