Using Hibernate with Spring

Hibernate has long been the persistence framework of choice for Spring developers. Although the Spring team continues to improve the integration with other persistence frameworks, Hibernate remains the most widely used persistence framework with Spring. It's fitting that these two lightweight solutions helped each other succeed. They go quite well together. In this example, we'll show how to integrate Spring and Hibernate.

Hibernate is an outstanding persistence framework. It's popular, it's relatively fast, and it's free. It has rich mapping support and a convenient usage model that's made it popular with developers across the world. Hibernate holds up very well in small- and intermediate-size projects. In fact, though it's nonstandard, you could say that behind EJB, Hibernate is the most popular persistence framework in the world.

How do I do that?

Now that you've configured Spring with an ORM, you know the basic flow, with a persistent domain model and a façade. Since Spring ships with Hibernate dependencies, you can just copy them from Spring's /dist folder to your /lib folder: hibernate2.jar, aopalliance.jar, cglib-full-2.0.2.jar, dom4j.jar, ehcache-1.1.jar, and odmg.jar.

Since Hibernate uses reflection, there's no byte code enhancement step. All you've got to do to make the model persistent is to create the mappings, and refer to them in the context. Examples Example 5-12, Example 5-13, and Example 5-14 show the mappings.

Example 5-12. Bike.hbm.xml ...

Get Spring: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.