Java Reference
In-Depth Information
Chapter 9. JPA entities
This chapter covers
• JPA and the impedance mismatch
• Domain models
• Implementing domain objects with JPA
• Defining relationships between domain objects with JPA
• Using inheritance with JPA domain objects
The Java Persistence API (JPA) is the Java standard to create, retrieve, update, and delete
(CRUD) data from relational databases. Java has had the ability to access databases almost
since its beginnings. The JDBC API gives developers direct access to the database for run-
ning SQL statements. Although powerful, JDBC is simple—it lacks many features. Because
of this, many proprietary object-relational mapping (O/R mapping) tools have been deve-
loped on top of JDBC. JPA standardizes O/R mapping for both the Java SE and Java EE
platforms. For developers, this means database applications created to these standards don't
include proprietary classes in the code.
The goal of this chapter is to give an overview of the most common and often used features
of JPA. First, we'll review how to turn any domain model POJO into an entity that JPA can
manage. After that, we'll look at how to map the entity to the database tables and columns
holding the data. Next, we'll examine the various strategies JPA has for specifying and gen-
erating database primary keys. Finally, we'll explore relationships between database tables
(for example, multiple Bid s on an Item ) and how JPA manages mapping these relation-
ships. Once you've learned how to do all of this, we'll discuss the EntityManager in the
next chapter and look at the code that actually performs all of the CRUD operations.
9.1. Introducing JPA
JPA is the Java standard solution for a problem commonly known as object-relational map-
ping. Simply put, O/R mapping is a translator. Imagine two people wish to communicate.
One speaks English and the other speaks French. They need a third party, a translator, who
knows both languages to communicate. O/R mapping is exactly the same thing, except one
language is your application's database and the other language is your application's Java do-
Search WWH ::




Custom Search