A Session in Hibernate is a lightweight, single-threaded object that represents one conversation, or unit of work, between the application and the database — it's the main API used to save, load, update, delete, and query entities.
Key Points: • Sessions are created by the SessionFactory and are cheap to open and close, unlike the SessionFactory itself. • A Session maintains a first-level cache (the persistence context) that tracks all persistent objects loaded or saved within it. • It is not thread-safe — each thread or unit of work should use its own Session instance. • Changes made to persistent objects are automatically detected and flushed to the database when the session flushes or the transaction commits. • Sessions should always be closed (typically in a finally block or try-with-resources) to release underlying database resources.
Example: In a web request, opening a Session at the start of the request, performing a few entity lookups and updates, committing the transaction, and closing the Session at the end is the typical "open session in view" or per-request pattern.
Interview Tip: A concise interview answer is:
"A Session is Hibernate's main runtime interface for persistence operations — it's a lightweight, single-threaded, short-lived object that acts as a staging area for changes, backed by a first-level cache, and it should be opened and closed per unit of work, not shared across threads."