Have a look at the article at
http://www.hibernate.org/110.html. It gives an overview of what values Spring can add to Hibernate. Two important values in the middle tier area are:
Configuration management
Spring can wire your entire middle tier in a loosely coupled fashion (yep, those IoC and AOP buzzwords ;-):
- resources like JDBC data sources and Hibernate session factories
- data access objects that access those resources
- business objects that delegate to your DAOs for persistence
This completely avoids the need for custom singletons or the like that hold configuration information, SessionFactory references, etc. You have one homogeneous style of configuration throughout your middle tier then. Spring cares for the entire lifecycle of your middle tier objects.
Spring provides means to link in JNDI resources into such definitions, wiring them with your application objects, without the latter being aware of JNDI. Alternatively, you can easily use local resource definitions, like a local Commons DBCP BasicDataSource.
Spring's LocalSessionFactoryBean is a convenient way of setting up a Hibernate SessionFactory in a Spring context, as an alternative to fetching the SessionFactory from JNDI. It provides all of Hibernate's configuration power, avoiding the need for a separate hibernate.cfg.xml file.
Transaction management
Transaction management is one of the most important Spring features. You have the choice between programmatic and declarative transaction demarcation, with pluggable transaction strategies: JTA, JDBC, Hibernate, JDO. (You just need JTA for actually distributed transactions.)
A popular usage is to proxy your business objects with Spring's AOP TransactionProxyFactoryBean, with HibernateTransactionManager as strategy. Each business service invocation will then automatically execute within a proper Hibernate transaction; DAOs will simply participate.
So Spring helps to separate transaction and data access concerns, avoiding the need for passing around Hibernate Sessions or using custom ThreadLocals. Multiple DAO invocations within a single transaction are seamless to achieve, reusing the same Hibernate Session.
Essentially, Spring's middle tier support offers similar services like local Session Beans but for POJOs - just like Hibernate offers persistence for POJOs as alternative to the Entity Beans component model. Spring and Hibernate are absolutely complementary here.
Juergen