Hibernate manages transactions through its Transaction interface, which acts as an abstraction over the underlying transaction mechanism — either plain JDBC transactions or the Java Transaction API (JTA) — so application code interacts with a single consistent API regardless of the environment.
Key Points: • session.beginTransaction() starts a Transaction, and commit()/rollback() finalize or undo it. • In a plain Java SE application, Hibernate typically delegates to JDBC's native transaction support (Connection.commit()/rollback()). • In managed environments (application servers, or Spring-managed contexts), Hibernate integrates with JTA to participate in distributed or container-managed transactions. • Spring applications commonly replace manual Transaction handling with declarative @Transactional annotations, letting Spring coordinate the transaction boundaries instead. • Auto-flushing before commit ensures all pending changes in the persistence context are written to the database as part of the same transaction.
Example: A plain Hibernate application explicitly calls session.beginTransaction(), performs several save/update calls, then tx.commit(), while a Spring Boot service method annotated @Transactional gets the same guarantee without ever touching the Transaction API directly.
Code Example:
Transaction tx = session.beginTransaction();
session.save(employee);
tx.commit();Interview Tip: A concise interview answer is:
"Hibernate exposes a Transaction interface that wraps either plain JDBC transactions or JTA depending on the environment, so beginTransaction(), commit(), and rollback() give a consistent API either way. In Spring applications this is usually abstracted further behind @Transactional rather than called directly."