Object-relational mapping and the Java Persistence API (JPA)

Study for Week 9

This week we will look at using Object-Relational Mapping (ORM) using the Java Persistence API (JPA).

Object-Relational Mapping is a way of automatically translating database records into Java objects. You can use ORM to replace your Data Access Objects and JDBC code.

The Java Persistence API (JPA) is the Object-Relational Mapping technology of Java EE.

At the end of this session, you should be able to:

  • Understand the limitations of JDBC and SQL
  • Explain the motivation and benefits of Object-Relational Mapping
  • Use JPA to create a simple database
  • Use the JPA EntityManager to perform Create, Read, Update and Delete operations
  • Write simple JPA queries
  • Understand the purpose of a persistence context and the distinction between managed and detached entities
  • Explain the differences between relational models and object oriented models that lead to the "Object-Relational Impedance Mismatch"

Object-relational mapping

What is object-relational mapping and why is it useful?

Entity annotations used in JPA

Entity manager

The EntityManager is the data access object in JPA:

Defining relationships in JPA


JPQL is the database query language provided by JPA - it very similar to SQL:

I recommend reading Chapters 4, 5, and 6 of Beginning Java EE 7: