What is Entity Manager?

4

What is the Java Entity Manager?

Looking at a lesson on java, the teacher mentioned that the% method of entity manager and that this method, when searching for a record in the database, saves the object in an area where it happens to be "monitored" and serves as the primary cache.

My question is what would this entity manager be? What is it used for? And what area is this?

    
asked by anonymous 04.09.2017 / 19:04

1 answer

7

In JPA, EntityManager is the class responsible for managing the entity lifecycle.

This class is able to:

  • Locate entities using the find method (which finds them through their primary keys). For example, if Funcionario is an entity class (annotated with @Entity ) and we want to get the Funcionario id = 123:

    EntityManager em = ...;
    Funcionario f1 = em.find(Funcionario.class, 123);
    
  • Run queries using JPQL or even native SQL. For example:

    String jpql = "SELECT f FROM Funcionario f WHERE f.empresa = :emp";
    EntityManager em = ...;
    Empresa emp = ...;
    List<Funcionario> funcionariosDaEmpresa = em
            .createQuery(jpql, Funcionario.class)
            .setParameter("emp", emp);
            .getResultList();
    
  • Persisting entities. For example:

    EntityManager em = ...;
    Funcionario f = new Funcionario();
    f.setNome("João Silva");
    em.persist(f);
    
  • Update entities in the database. For example:

    EntityManager em = ...;
    Funcionario f = em.find(Funcionario.class, 123);
    f.setCorFavorita("Amarelo");
    em.merge(f);
    
  • Update entities from the database. For example:

    EntityManager em = ...;
    Funcionario f = em.find(Funcionario.class, 123);
    
    // ...
    // ... Algum outro processo altera o estado do funcionário 123 aqui.
    // ...
    
    // Atualiza o estado da instância na memória de acordo com o que há no banco de dados.
    em.refresh(f);
    
  • Remove entities from the database. For example:

    EntityManager em = ...;
    Funcionario f = em.find(Funcionario.class, 123);
    em.remove(f);
    

An instance of a EntityManager can be obtained in two ways:

  • Dependency injection (typical case used in Spring and EJBs):

    @Stateless
    public class MeuBean {
        @PersistenceContext(unit = "testePU")
        EntityManager em;
    
        public void meuMetodo() {
            // Usa o em aqui.
        }
    }
    
  • Through EntityManagerFactory and of the class Persistence :

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("testePU");
    EntityManager entityManager = factory.createEntityManager();
    
  • EntityManager maintains the entities it manages in a state called managed - these are the entities it monitors. These entities are those that it creates through the find method, through JPQL or that it receives in the persist or merge method. It performs caching of these entities, so reading twice the same entity from the database will not produce two different instances - the same instance is returned in the second reading.

    However, not all instances of entities are managed - namely those that have just been instantiated and have not yet been passed to EntityManager (new state) those that were excluded using the remove (removed state) method and those that were unlinked from EntityManager through the methods clear() or detach(Object) (status detached ).

    See more about JPA, including EntityManager , this link .

        
    04.09.2017 / 19:18