In a Hibernate application, @Transactional is not a Hibernate annotation. It comes from Spring (or Jakarta Transactions) and tells the framework to open a transaction, bind a Hibernate Session to it, flush pending changes on commit and roll back on failure. The significance for Hibernate is that the transaction defines the lifetime of the persistence context: entities are managed, changes are detected and written, and lazy associations load only while that transaction is active.
This article explains how Spring and Hibernate cooperate, what the transaction boundary means for the Session, and the mistakes that cause lost updates or LazyInitializationException.
Hibernate on its own requires you to call session.beginTransaction(), then commit() or rollback(), and to close the session. When you use Spring, the transaction manager does this. With Spring Boot and JPA the auto-configured JpaTransactionManager starts a transaction on the EntityManager (which is a Hibernate Session underneath), and the connection stays tied to the current thread until the transaction ends.
The EntityManager injected here is a shared proxy that always delegates to the one bound to the current transaction. If you run Hibernate natively with a SessionFactory, use sessionFactory.getCurrentSession(). It only works inside a Spring-managed transaction, and calling it without one throws an exception.
Inside the transaction, every loaded entity is tracked. When the transaction commits, Hibernate flushes: it compares each managed entity against its loaded snapshot and issues UPDATE statements for the ones that changed. You do not call save or update for managed entities.
Within one transaction, loading the same row twice returns the same instance and runs one query. That cache is discarded when the session closes, so it never leaks between requests.
Lazy collections and proxies need an open session. Inside the transactional method you can call customer.getOrders().size(). After the method returns and the session is closed, the same call throws LazyInitializationException. Load what you need before leaving the service, using JOIN FETCH, an entity graph or a DTO projection.
An unchecked exception rolls the database transaction back. The Hibernate Session is then in an undefined state: entities may still hold values from before the failure, and the session should not be reused. Spring closes it at the end of the transaction, so the practical rule is to let the exception propagate rather than catching it inside the same transaction and continuing.
Checked exceptions commit by default. If a failure must undo the work, declare it:
| Attribute | Effect with Hibernate |
|---|---|
readOnly = true | Spring sets the session flush mode to manual, so dirty checking is skipped and the unit of work is cheaper |
isolation | Passed to the JDBC connection; the database must support the level |
timeout | Sets a query timeout, then marks the transaction for rollback |
propagation = REQUIRES_NEW | Opens a second session and connection while the first waits |
Prefer readOnly = true for query methods, but do not write inside them: changes are never flushed. Be careful with REQUIRES_NEW in a busy pool, because one request then needs two connections at the same time, which can exhaust the pool under load.
TransactionRequiredException on writes.@Transactional on an entity or a plain object not managed by Spring.open-in-view, which hides lazy loading queries and keeps a connection for the full request.HibernateTransactionManager and a JpaTransactionManager for the same DataSource in one app.No. Hibernate has its own Transaction API. @Transactional is a Spring annotation (and a Jakarta Transactions one) that drives that API through a transaction manager.
Normally not. Hibernate flushes on commit and, with the default auto flush mode, before queries that could be affected. persist is needed only for new entities.
The session closed when the transaction ended. Fetch the data inside the transactional method, or map entities to DTOs before returning.
It does not enforce anything by itself. Spring and Hibernate use it as a hint to skip flushing, and some drivers pass it on to the database.
For Hibernate, @Transactional sets the boundary of the session: entities are tracked, flushed and committed together, or rolled back together. Use it on public service methods, keep lazy loading inside the boundary, mark query methods read-only, and avoid long transactions. Rollback applies to unchecked exceptions unless you configure rollbackFor.