1. You must distinct classes from instances. Yes, all the entity classes(!) are loaded into memory, when the SessionFactory build's up the metadata-information. And yes, entity instances(!) are loaded only as and when required based on lazy initialization property.
Classes are loaded by a class-loader and remain usually in memory unless the JVM is in risk of running out of memory. In this situation during the GC, the GC may unload unused classes again.
Entity instances indeed are garbage collected in the same way as any other common java object, that means the are garbage collected when there is no more reference pointing to the object instance. In other words: Once you clear the persistent context or close the session, entity instances are free to be garbage collected if you don't refer them anymore from your application.
>>3> If so does it mean, on consequent opening of a session, all the entity classes loaded again? No. Neither classes nor entity instances are loaded, only by opening a new session again.
|