Search code examples
javahibernatejpajpa-2.0entitymanager

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!


Solution

  • 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.