jpa

Topics related to jpa:

Getting started with jpa

JPA is the Java Persistence API, a specification handling the mapping of Java objects and their relationships to a relational database. This is called an object-relational mapper (ORM). It is an alternative for (or supplement to) the more low-level JDBC. It is most useful when pursuing a Java-oriented approach and when complex object graphs need to be persisted.

JPA in itself is not an implementation. You will need a persistence provider for that (see examples). Current implementations of the latest JPA 2.1 standard are EclipseLink (also the reference implementation for JPA 2.1, which means "proof that the spec can be implemented"); Hibernate, and DataNucleus.

Metadata

The mapping between Java objects and database tables is defined via persistence metadata. The JPA provider will use the persistence metadata information to perform the correct database operations. JPA typically defines the metadata via annotations in the Java class.

Object-Relational Entity Architecture

The entity architecture is composed of:

  • entities
  • persistence units
  • persistence contexts
  • entity manager factories
  • entity managers

Basic mapping

There always needs to be a default constructor, that is, the parameterless one. In the basic example, there was no constructor specified, so Java added one; but if you add a constructor with arguments, be sure to add the parameterless constructor, too.

Table per concrete class inheritance strategy

Single Table Inheritance Strategy

Relations between entities

Relations Between Entities Basics

A foreign key can be one or more columns that reference a unique key, usually the primary key, in another table.

A foreign key and the primary parent key it references must have the same number and type of fields.

Foreign keys represents relationships from a column or columns in one table to a column or columns in another table.

Joined Inheritance strategy

One to One mapping

One to Many relationship

Many To One Mapping

Many to Many Mapping