Chapter 2. Data persistence, ORM, and transactions


This chapter covers

  • Understanding Spring JDBC data-persistence templates
  • Exploiting ORM using Hibernate and JPA
  • Learning how transactions manage database concurrency

This chapter assembles the data persistence, ORM, DAO, and transaction-management infrastructure you’ll be using throughout the rest of the book. Although there are cases where it’s useful to work directly with JDBC, the ORM approach confers major benefits in terms of simplifying the codebase. Most of the persistence examples throughout the book are ORM-based—specifically Hibernate-based—and so a large part of what we’ll do here is explain not only how ORM fits into the scheme of things, but also how to perform common tasks with Hibernate specifically. We don’t pretend to offer an exhaustive treatment of Hibernate, but we hope that it’s sufficient to allow you to make sense of the code examples and get started with Hibernate if you aren’t already using it.[1]

1 For the definitive reference on Hibernate, see Java Persistence with Hibernate by Christian Bauer and Gavin King (Manning 2006),

Here’s an overview of what we’ll be doing in this chapter:

2.1. Data access using JDBC

2.2. Looking up a DataSource with JNDI

2.3. Object-relational mapping and transactions via Hibernate

2.4. Creating a data access layer

2.5. Working with JPA (optional)

2.6. Spring Data JPA overview (optional)

2.7. Summary