I've been trying to learn spring and hibernate, and I've used a lot of examples around the net to put together a nice application. However, I realized now that Spring supports transaction management. In my spring app I just simply made whatever calls I wanted to, directly to hibernate. Is there a reason/benefit as to why people would use Spring's transaction management/db stuff?
-
2Are you using two phase commits to a database as well as to somewhere else - Use Spring. else pick the one you want to use. – Romain Hippeau Feb 09 '11 at 03:11
-
What is a two phase commit if I might ask? – Matthew Stopa Feb 10 '11 at 04:16
2 Answers
The real advantages are:
Lightweight declarative syntax. Compare:
public void saveEmployee(Employee e) { Session s = sf.getCurrentSession(); s.getTransaction().begin(); s.save(e); s.getTransaction().commit(); }
and
@Transactional public void saveEmployee(Employee e) { sf.getCurrentSession().save(e); }
Flexible transaction propagation. Imagine that now you need to execute this
saveEmployee()
method as a part of a complex transaction. With manual transaction management, you need to change the method since transaction management is hard-coded. With Spring, transaction propagation works smoothly:@Transactional public void hireEmployee(Employee e) { dao.saveEmployee(e); doOtherStuffInTheSameTransaction(e); }
Automatic rollback in the case of exceptions

- 239,438
- 41
- 511
- 482
-
I guess I should ask then, what is the benefit of using hibernate with Spring? Does Hibernate still bring something to the table? – Matthew Stopa Feb 10 '11 at 04:18
-
@John: Compared to Spring without Hibernate or Hibernate without Spring? – axtavt Feb 10 '11 at 08:42
-
1I'm trying to understand why someone would actually use Hibernate if they had Spring. It doesn't seem that Hibernate is buying them much. Or maybe I'm missing something. – Matthew Stopa Feb 11 '11 at 02:38
The spring's reference doc mentions it very clear
The Spring Framework provides a consistent abstraction for transaction management that delivers the following benefits:
- Provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.
- Supports declarative transaction management.
- Provides a simpler API for programmatic transaction management than a number of complex transaction APIs such as JTA.
- Integrates very well with Spring's various data access abstractions.
Provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.
Let us say you are currently using hibernate api for transaction management and sometime down the road you would want to switch to JDO. This requires the change of transaction management code. If you use Spring then there is no change.
Supports declarative transaction management.
Similar to EJB
Provides a simpler API for programmatic transaction management than a number of complex transactions APIs such as JTA.
JTA and JDBC have different APIs for transaction management. Spring abstracts that out by providing a uniform API.

- 36,381
- 5
- 80
- 102

- 78,777
- 46
- 231
- 327
-
Yes but in real terms I'm not sure what that means. Is it standard practice to use Spring's transaction management over Hibernates? Aren't you missing some of the benefits of using hibernate this way? – Matthew Stopa Feb 09 '11 at 02:58
-
2@John Baker - No, you are not. Spring's HibernateTransactionManager (unsurprisingly) uses the features of Hibernate's transactions, while maitaining the advantage of Spring's unified transaction abstractions. If you're using Spring and you're using transactions, then you should use Spring's transactions. – OrangeDog Feb 09 '11 at 11:10