📕
Knowledge
  • Knowledge Repository
  • Java
    • Intro
    • Persistence
      • Java Persistence API (JPA)
        • Entity Manager
        • Transaction Management
        • Relationship Mapping
      • Java Transaction API (JTA)
    • Resources
  • Angular
    • Intro
    • Overview
    • CLI Commands
    • Directives
  • Tools
    • IDEs
      • IntelliJ
        • Shortcuts
  • Book Notes
    • Intro
    • Java A Beginner's Guide
      • Chapter 1. Java Fundamentals
      • Chapter 2. Data Types and Operators
      • Chapter 3. Program Control Statements
      • Chapter 4. Introducing Classes, Objects, and Methods
      • Chapter 5. More Data Types & Operators
      • Chapter 6. Closer Look At Methods and Classes
      • Chapter 7. Inheritance
      • Chapter 8. Packages & Interfaces
      • Chapter 9. Exception Handling
      • Chapter 10. Using I/O
    • Data Pipelines With Airflow
      • Chapter 1. Meet Airflow
      • Chapter 2. Anatomy Of a DAG
  • Course Notes
    • Intro
    • Spring: TDD With JUnit
Powered by GitBook
On this page
  • Application Layer
  • Using the Entity Manager
  • Reading Entities
  • Persisting Entities
  • Updating Entities
  • Deleting Entities

Was this helpful?

  1. Java
  2. Persistence
  3. Java Persistence API (JPA)

Entity Manager

The Entity Manager API is used to create, uodate, remove and find entity instances.

Entities are managed by the Entity Manager and until an entity is managed it is considered to be a Plain Old Java Oject (POJO), in detached state.

Once it's managed, it is now in a managed state.

Notes to be updated regarding Entity Manager

Application Layer

You'll typically find the entity manager usage, or other persistent functionality in the DAO or repository layers/classes.

Using the Entity Manager

Reading Entities

When retrieving an entity using JPA, it's the entity manager that performs this operation, using the available find method. There are several variations on the find method, but typical it is used with an entity's primary key.

    public Entity getEntityById(String entityId) {
        return entityManager.find(Entity.class, entityId);
    }

Persisting Entities

To persist an entity, use the available persist method.

    public void addEntity(Entity entity) {
        entityManager.persist(entity);
    }

Updating Entities

Note, there is no specific update method to call in the entity manager class, an update happens when one of the entities attribute values changes.

First you use the find method to return the persisted entity, and then you update its values before re-persisting.

    public void updateEntity(Entity entity) {
        Entity getEntity = getEnityById(entity.getId());
        getEntity.setName(entity.getName());
        entityManager.flush();
    }

Deleting Entities

To delete an entity, use the available remove method.

    public void deleteEntity(String entityId) {
        entityManager.remove(getEntityById(entityId));
    }
PreviousJava Persistence API (JPA)NextTransaction Management

Last updated 4 years ago

Was this helpful?