Spring Data JPA: EntityManager Explained!

Understanding EntityManager, Entity, Persistence Context

Suraj Mishra
Javarevisited

--

Introduction

  • EntityManager is one of the key abstractions that JPA specification defines. EntityManager sits between the database and the application and plays the responsibility of managing the entity in context.
  • In this article, we will explore these responsibilities and try to learn about EntityManager.

JPA Architecture

Credits: https://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/jpa_overview_arch.html

Entity

  • An Entity is basically a table representation of a relational database and an instance of that entity corresponds to a row in that table.
  • It's a domain object in a persistent context.

Example of an Entity

  • @Entity is an annotation that we use to mark a POJO to the persistent context-aware entity.
  • @Table allows us to map entities to any custom-named database.
  • Other such annotations are @Id @SequenceGenerator @Column etc. that can be used inside the entity.
@Entity
@Table(name="ACCOUNTS")
public class Account {
@Id @GeneratedValue(strategy= GenerationType.SEQUENCE, generator = "accounts_seq")
@SequenceGenerator(name = "accounts_seq", sequenceName =…

--

--