What is the difference between FetchType.LAZY and FetchType.EAGER? FetchType.LAZY और FetchType.EAGER में क्या अंतर है?
FetchType.LAZY defers loading a related entity or collection until it is actually accessed in code, returning a proxy initially. FetchType.EAGER loads the related data immediately, in the same query or an immediate follow-up query, whenever the owning entity is loaded.
Lazy loading is generally preferred for performance since it avoids fetching unnecessary data, but accessing a lazy association outside an active persistence context (e.g. after the transaction closes) throws a LazyInitializationException. @OneToMany and @ManyToMany default to LAZY; @ManyToOne and @OneToOne default to EAGER, which is why they're often explicitly overridden to LAZY.
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private Author author;FetchType.LAZY, संबंधित एंटिटी या कलेक्शन को लोड करना तब तक टालता है जब तक उसे कोड में वास्तव में एक्सेस न किया जाए, शुरुआत में एक प्रॉक्सी रिटर्न करता है। FetchType.EAGER, ओनिंग एंटिटी लोड होते ही संबंधित डेटा को तुरंत लोड कर देता है।
लेज़ी लोडिंग आमतौर पर परफॉर्मेंस के लिए बेहतर मानी जाती है क्योंकि यह अनावश्यक डेटा फेच करने से बचाती है, लेकिन ट्रांज़ैक्शन बंद होने के बाद लेज़ी एसोसिएशन एक्सेस करने पर LazyInitializationException आती है। @OneToMany और @ManyToMany डिफ़ॉल्ट रूप से LAZY होते हैं; @ManyToOne और @OneToOne डिफ़ॉल्ट रूप से EAGER होते हैं।
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private Author author;Was this answer clear?