If a class is marked as @Transactional, does that mean that the boilerplate:
Transaction tx = session.getTransaction();
try {
...
...
...
tx.commit();
}
catch (Exception e) {
tx.rollBack();
}
can be omitted? My Hibernate-based code certainly works without it.
EDIT (FOLLOW UP): Then why do people keep writing the boilerplate within their methods, even when they have their classes annotated?
Yes, as long as
Transaction management is properly configured, see 13.3.3 Declarative transaction demarcation
Calls to that method don’t violate limitations of AOP-based declarative transaction management, see 10.5.1 Understanding the Spring Framework’s declarative transaction implementation (or you use advanced features such as AspectJ integration to overcome these limitations).
That is, all calls to that method are performed on an object obtained from Spring, and no calls are performed from another methods of the same class.
Sessionis obtained inside a method by callingSessionFactory.getCurrentSession().