Almost. You need just one EntityManagerFactory per persistence unit.
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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.