Topic 9
■■■
Using Hibernate in Spring
In the previous chapter, you saw how to use JDBC in Spring applications. However, even though Spring
goes a long way toward simplifying JDBC development, you still have a lot of code to write.
In this chapter, we cover one of the object-relational mapping (ORM) libraries that has wide support
in Spring--Hibernate.
If you have experience developing data access applications using EJB entity beans (prior to EJB 3.0),
you may remember the painful process. Tedious configuration of mappings, transaction demarcation,
and much boilerplate code in each bean to manage its life cycle greatly reduced the productivity when
developing enterprise Java applications.
Just like Spring was developed to embrace POJO base development and declarative configuration
management rather than EJB's heavy and clumsy setup, the developer community realize that a simpler,
lightweight, and POJO base framework could ease the development of data access logic. Since then,
many different libraries have appeared; they are generally referred to as ORM libraries. The main
objective of an ORM library is to close the gap between the relational data structure in the RDBMS and
the OO model in Java so that developers can focus on programming with the object model and at the
same time easily perform actions related to persistence.
Of the ORM libraries available in the open source community, Hibernate is one of the most
successful. Its features, such as POJO base approach, ease of development, and support of sophisticated
relationship definitions, have won the heart of the mainstream Java developer community.
Hibernate's popularity has also affected the JCP, which developed the Java Data Objects (JDO)
specification as one of the standard ORM technologies in Java EE. Starting from EJB 3.0, the EJB entity
bean was even replaced with the Java Persistence API (JPA), within which a lot of the ideas were
influenced by popular ORM libraries such as Hibernate, TopLink, and JDO.
The relationship between Hibernate and JPA is very close. Gavin King, the founder of Hibernate,
represented JBoss as one of the JCP expert group members in defining the JPA specification. Starting
from version 3.2, Hibernate has provided an implementation of JPA. So, when you develop applications
with Hibernate, you can choose to use either Hibernate's own API or the JPA with Hibernate as the
persistence service provider.
Having discussed a rough history of Hibernate, this chapter will cover how to use Spring with
Hibernate when developing data access logic. Hibernate is such an extensive ORM library, so covering
every aspect of Hibernate in just one chapter is simply not possible, and numerous books are dedicated
to discussing Hibernate.
This chapter will cover the basic ideas and main use cases of using Hibernate in Spring. In
particular, we are going to discuss the following topics:
Configuring the Hibernate SessionFactory: The core concept of Hibernate
·
revolves around the Session interface, which is managed by the SessionFactory.
We will discuss how to configure Hibernate's session factory to work in a Spring
application.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home