Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA 2 - Using the EntityManager in JavaSE - a couple of questions

I have a couple questions regarding using the entity manager in a JavaSE environment.

I'm using the repository pattern to perform my CRUD operations on the database. There will be a base repository class like so:

public class Repository<T> implements IRepository<T> {

    private EntityManager em;
    private String persistenceUnitName;

    public Repository(String persistenceUnitName) {
        this.persistenceUnitName = persistenceUnitName;
    }

    @Override
    public T find(Class<T> type, Object id) {
        return em.find(type, id);
    }

    private EntityManager getEntityManager() {
        if (this.em == null) {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName);

            em = emf.createEntityManager();
        }

        return em;
    }
    ...
    ...
}

I will then have classes like EmployeeRepository that will inherit Repository. These repository classes will be created in my service layer.

Is this a good way of initializing the entity manager? I'm starting to think that it's not - it seems like you should only have one entity manager per persistence unit? Where as in this case you would have an entity manager for every repository you create... How would you go about ensuring you only have one entity manager per persistence unit? Also, I noticed that the entity manager and entity manager factory methods have a close method - when should these be called? On a server terminate event?

If you know of any good sources about using JPA in JavaSE I would appreciate the info.

Thanks!

like image 306
Brian DiCasa Avatar asked Nov 01 '10 01:11

Brian DiCasa


People also ask

What is the use of EntityManager in JPA?

In JPA, the EntityManager interface is used to allow applications to manage and search for entities in the relational database. The EntityManager is an API that manages the lifecycle of entity instances. An EntityManager object manages a set of entities that are defined by a persistence unit.

What is EntityManager in Spring data JPA?

EntityManager is part of the Java Persistence API. Chiefly, it implements the programming interfaces and lifecycle rules defined by the JPA 2.0 specification. Moreover, we can access the Persistence Context by using the APIs in EntityManager.

What is the use of EntityManager in Hibernate?

The EntityManager API is used to access a database in a particular unit of work. It is used to create and remove persistent entity instances, to find entities by their primary key identity, and to query over all entities.


1 Answers

Almost. You need just one EntityManagerFactory per persistence unit.

How would you go about ensuring you only have one EntityManagerFactory per persistence unit ?

Usually developers create an helper class with a singleton EntityManagerFactory such as

public class EntityManagerFactoryHelper {

    private static EntityManagerFactory factory;

    static {
        try {
           // Set up factory right here
        } catch(ExceptionInInitializerError e) {
            throw e;
        }
    }

    public static EntityManagerFactory getFactory() {
        return this.factory;
    }

}

EntityManager, in other hand, is used to interact with a set of managed entity instances called persistence context.

If you want to know why i use ErrorInInitializerError, its API is clear

Signals that an unexpected exception has occurred in a static initializer

...

Is this a good way of initializing the entity manager ?

Well, the service layer is used to delimit Transaction boundary. So for each use case, you can create your EntityManager and pass by reference for each colaborator needed to help you to execute your use case.

public static br.com.helper.EntityManagerFactoryHelper.getFactory;

public EmployeeService {

    public void doSomething() {
        EntityManager eManager = getFactory().createEntityManager();
        eManager.getTransaction().begin();

        EmployeeRepository repository = new EmployeeRepository(eManager);

        eManager.getTransaction().commit();
    }

}

Now imagine you need the boilerplate code shown above for each use case.

    public void forEachUseCase() {
        // Create an EntityManager
        // Begin a Transaction

        EmployeeRepository repository = new EmployeeRepository(eManager);

        // And finally, commit
    }

You can rely on Spring to help you to get rid of this boilerplate code.

like image 190
Arthur Ronald Avatar answered Sep 28 '22 02:09

Arthur Ronald