views:

61

answers:

3

Hi! I'm just wondering why the combination of Spring and Hibernate is so popular, when it would be possible to leave Hibernate out and use just Spring ORM?

+6  A: 

Spring doesn't have an ORM of its own. You can use Hibernate, TopLink, iBatis, or JDO.

You are free to use Spring JDBC, but you have to write all the mappers on your own.

It's advisable to leave ORM out of the loop if you don't like dynamic SQL or the complexity of an ORM solution. You can also eliminate a dependency by sorting out persistence on your own.

duffymo
+2  A: 

Spring is popular because it takes care of the 'boilerplate' cut and paste code you have with any ORM framework. Think try ... finally blocks, dealing with the session object (Hibernate or otherwise) and commit / rollback (transactions).

Transaction management is also Spring's strength. You can define transactions using annotations or in the Spring xml config file. In the config file, the benefit is that you can use wildcards to specify that, for example, all find methods in some set of packages should support transactions (PROPAGATION_SUPPORTS) but all insert, update, delete methods should require transactions (PROPAGATION_REQUIRED).

So, I would always use Spring, regardless of the ORM framework. If you have simple requirements or not that much JDBC code, Spring's JDBC templates may be enough for you. And, Spring makes it easy to upgrade to Hibernate when needed.

AngerClown
+1  A: 

The other answers are really good. But to answer the 'why is Spring/Hibernate' so popular - IMHO its because they are best of breed technologies that allow you to go faster. Spring does a lot more than just dependency injection and transactions. The provide solutions for JMS, scheduling, and other things. Plus, they are battle tested and well written. They allow you to focus on your business needs, and make some of the hard stuff go away.

All of this is not to say that you don't need good developers to implement a good solution.

hvgotcodes