Java Reference
In-Depth Information
CHAPTER 1
n n n
An Introduction to Hibernate 3
M ost significant development projects involve a relational database. The mainstay of most
commercial applications is the large-scale storage of ordered information, such as catalogs,
customer lists, contract details, published text, and architectural designs.
With the advent of the World Wide Web, the demand for databases has increased. Though
they may not know it, the customers of online bookshops and newspapers are using databases.
Somewhere in the guts of the application a database is being queried and a response is offered.
While the demand for such applications has grown, their creation has not become notice-
ably simpler. Some standardization has occurred—the most successful being the Enterprise
JavaBeans (EJB) standard of Java 2 Enterprise Edition ( J2EE), which provides for container-
and bean-managed persistence of entity bean classes. Unfortunately, this and other persist-
ence models all suffer to one degree or another from the mismatch between the relational
model and the object-oriented model. In short, database persistence is difficult.
There are solutions for which EJBs are appropriate, some for which some sort of object-
relational mapping (ORM) like Hibernate is appropriate, and some for which the traditional
approach of direct access via the Java Database Connectivity ( JDBC) API is appropriate. We
think that Hibernate represents a good first choice, as it does not preclude the simultaneous
use of these alternative approaches.
To illustrate some of Hibernate's strengths, in this chapter we will show you a brief exam-
ple using Hibernate and contrast this with the traditional JDBC approach.
Plain Old Java Objects (POJOs)
In our ideal world, it would be trivial to take any Java object and persist it to the database. No
special coding would be required to achieve this, no performance penalty would ensue, and
the result would be totally portable.
In this ideal world, we would perhaps perform such an operation in a manner like that
shown in Listing 1-1.
Listing 1-1. A Rose-Tinted View of Object Persistence
POJO pojo = new POJO();
ORMSolution magic = ORMSolution.getInstance();
magic.save(pojo);
There would be no nasty surprises, no additional work to correlate the class with tables in
the database, and no performance problems.
1
Search WWH ::




Custom Search