Explain the concept of Object States in Hibernate.

Hibernate tracks every entity instance as being in one of three lifecycle states relative to a Session: transient, persistent, or detached. Understanding which state an object is in determines whether Hibernate will track its changes and sync them to the database.

Key Points: • Transient: a plain object created with new that has no identifier and is not associated with any Session — Hibernate knows nothing about it. • Persistent: the object is associated with an open Session and has a database identity; any changes to it are automatically tracked and flushed to the database. • Detached: the object once had a Session association, but that Session has since been closed or the object evicted; changes are no longer tracked until it's reattached. • session.save()/persist() moves an object from transient to persistent. • session.merge() reattaches a detached object's state into a new persistent instance.

Example: A new Employee() object is transient; after session.save(employee) it becomes persistent and updates to its fields are auto-flushed; if the session is later closed, that same object becomes detached, and any field changes made after that point are silently ignored until it's merged back into a new session.

Interview Tip: A concise interview answer is:

"Hibernate entities move through three states: transient, meaning just a plain new object with no session association; persistent, meaning attached to an open session so changes are automatically tracked and saved; and detached, meaning it was once persistent but the session closed, so changes need an explicit merge to be picked up again."