Topic 10
■■■
Data Access in Spring with JPA2
In the previous chapter, we discussed how to use Hibernate in Spring applications when implementing
data access logic with the ORM approach. We demonstrated how to configure Hibernate's SessionFactory
in Spring's configuration and how to use Hibernate's Session interface for various data access operations.
However, what we discussed in the previous chapter was just one aspect of Hibernate's usage.
Another way of adopting Hibernate in a Spring application is to use Hibernate as a persistence provider
of the JCP standard, the Java Persistence API (JPA).
As discussed earlier, Hibernate's POJO base mapping and its powerful query language (HQL) have
gained great success and have influenced the development of data access technology standards in the
Java world. After Hibernate, the JCP developed the Java Data Objects (JDO) standard and then JPA.
At the time of this writing, JPA has reached 2.0 (which is part of the JEE 6 technology stack); it is a
much more mature technology and is being widely adopted by developers as the data access API
standard. This is because JPA has standardized the ORM programming model with concepts such as
PersistenceContext, EntityManager, and the Java Persistence Query Language (JPQL). These
standardizations provide a way for developers to switch between JPA persistence providers such as
Hibernate, EclipseLink, Oracle TopLink, OpenJPA, and so on. As a result, most new JEE applications are
now adopting JPA as the data access layer.
Spring also provides intensive support for JPA. For example, a number of
EntityManagerFactoryBeans are provided for bootstrapping a JPA entity manager in a Spring application,
with support for all of the JPA providers mentioned earlier. The Spring Data project also provides a
subproject called Spring Data JPA, which provides advanced support for using JPA in Spring
applications. The main features of the Spring Data JPA project include the concepts of Repository and
Specification, support for the Query Domain Specific Language (QueryDSL), and so on.
In this chapter, we will discuss how to use JPA (specifically JPA 2) with Spring, using Hibernate as
the underlying persistence provider. You will learn how to implement the various database operations
using JPA's EntityManager interface and JPQL (which is similar to HQL). Then we will discuss how Spring
Data JPA can further help simplify JPA development. Finally, we will discuss some advanced topics
related to ORM, including native queries and criteria queries.
Specifically, we will discuss the following topics:
Core concepts of JPA: We will cover some of the major concepts of JPA.
·
Creating a simple Spring JPA utility project: We will go through the steps for
·
creating a Spring-based JPA project using STS. Although STS creates the project
with required dependencies, we still need to fine-tune the project configurations
in order to support all the features discussed in this chapter. As we move on to
more advanced topics, additional third-party libraries will be required, so we will
demonstrate how to add them in STS.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home