Java Reference
In-Depth Information
C H A P T E R 3
■ ■ ■
Data Access
In this chapter, you will learn how Spring can simplify your database access tasks. Data access is a
common requirement for most enterprise applications, which usually require accessing data stored in
relational databases. As an essential part of Java SE, JDBC (Java Database Connectivity) defines a set of
standard APIs for you to access relational databases in a vendor-independent fashion.
The purpose of JDBC is to provide APIs through which you can execute SQL statements against a
database. However, when using JDBC, you have to manage database-related resources by yourself and
handle database exceptions explicitly. To make JDBC easier to use, Spring provides an abstraction
framework for interfacing with JDBC. As the heart of the Spring JDBC framework, JDBC templates are
designed to provide template methods for different types of JDBC operations. Each template method is
responsible for controlling the overall process and allows you to override particular tasks of the process.
If raw JDBC doesn't satisfy your requirement or you feel your application would benefit from
something slightly higher level, then Spring's support for ORM solutions will interest you. In this
chapter, you will also learn how to integrate object/relational mapping (ORM) frameworks into your
Spring applications. Spring supports most of the popular ORM (or data mapper) frameworks, including
Hibernate, JDO, iBATIS, and JPA (the Java Persistence API). Classic TopLink isn't supported starting
from Spring 3.0 (the JPA implementation's still supported, of course). However, the JPA support is
varied, and has support for many implementations of JPA, including the Hibernate and TopLink-based
versions. The focus of this chapter will be on Hibernate and JPA. However, Spring's support for these
ORM frameworks is consistent, so you can easily apply the techniques in this chapter to other ORM
frameworks as well.
ORM is a modern technology for persisting objects into a relational database. An ORM framework
persists your objects according to the mapping metadata you provide (XML or annotation-based),
such as the mappings between classes and tables, properties and columns, and so on. It generates
SQL statements for object persistence at runtime, so you needn't write database-specific SQL statements
unless you want to take advantage of database-specific features or provide optimized SQL statements of
your own. As a result, your application will be database independent, and it can be easily migrated to
another database in the future. Compared to the direct use of JDBC, an ORM framework can
significantly reduce the data access effort of your applications.
Hibernate is a popular open source and high-performance ORM framework in the Java community.
Hibernate supports most JDBC-compliant databases and can use specific dialects to access particular
databases. Beyond the basic ORM features, Hibernate supports more advanced features such as caching,
cascading, and lazy loading. It also defines a querying language called Hibernate Query Language (HQL)
for you to write simple but powerful object queries.
JPA defines a set of standard annotations and APIs for object persistence in both the Java SE and
Java EE platforms. JPA is defined as part of the EJB 3.0 specification in JSR-220. JPA is just a set of
standard APIs that require a JPA-compliant engine to provide persistence services. You can compare JPA
Search WWH ::

Custom Search