Reputation: 9487
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!
Upvotes: 5
Views: 15677
Reputation: 1941
Brian,
It is okay to share EntityManagerFactory, but it is not okay to share EntityManager. See the Java Persistence wikibook.
A more modern approach would be to do singletons with enums. So, assuming you don't expect to go around changing your persistence unit names (I would expect that not to occur anyhow)...
So ideally...
Using an enum for the singleton mechanism provides for... (Effective Java Second edition)
public enum PersistenceUnitFactory { PERSISTENCE_UNIT_1("persistenceUnit1"), PERSISTENCE_UNIT_2("persistenceUnit2"); private final EntityManagerFactory emf; /** * Maps the persistence unit name to this instance of the enum. * * @param persistenceUnitName the name of the peristence unit in * persistence.xml * * @see #getPersistenceUnitName() */ PersistenceUnitFactory(final String persistenceUnitName) { emf = Persistence.createEntityManagerFactory(persistenceUnitName); } /** * Creates a new {@link EntityManager}. NEVER share this across threads. * * @return a new {@link EntityManager} */ EntityManager createEntityManager() { return emf.createEntityManager(); } }
Then it's as simple as asking for an EntityManager...
PersistenceUnitFactory.PERSISTENCE_UNIT_1.createEntityManager();
You can add more persistence units as you need them.
Upvotes: 2
Reputation: 33775
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.
Upvotes: 4