@Test public void testEntityManager() { EntityManager em = JPASessionUtil.getEntityManager("utiljpa"); em.getTransaction().begin(); Thing t = new Thing(); t.setName("Thing 1"); em.persist(t); em.getTransaction().commit(); em.close(); em = JPASessionUtil.getEntityManager("utiljpa"); em.getTransaction().begin(); TypedQuery<Thing> q = em.createQuery("from Thing t where t.name=:name", Thing.class); q.setParameter("name", "Thing 1"); Thing result = q.getSingleResult(); assertNotNull(result); assertEquals(result, t); em.remove(result); em.getTransaction().commit(); em.close(); }
@SuppressWarnings("WeakerAccess") public static synchronized EntityManager getEntityManager(String persistenceUnitName) { persistenceUnits.putIfAbsent(persistenceUnitName, Persistence.createEntityManagerFactory(persistenceUnitName)); return persistenceUnits.get(persistenceUnitName) .createEntityManager(); }
JPASessionUtil { @SuppressWarnings("WeakerAccess") public static synchronized EntityManager getEntityManager(String persistenceUnitName) { persistenceUnits.putIfAbsent(persistenceUnitName, Persistence.createEntityManagerFactory(persistenceUnitName)); return persistenceUnits.get(persistenceUnitName) .createEntityManager(); } }
JPASessionUtil { @SuppressWarnings("WeakerAccess") public static synchronized EntityManager getEntityManager(String persistenceUnitName) { persistenceUnits.putIfAbsent(persistenceUnitName, Persistence.createEntityManagerFactory(persistenceUnitName)); return persistenceUnits.get(persistenceUnitName) .createEntityManager(); }  }
JPASessionUtil { @SuppressWarnings("WeakerAccess") public static synchronized EntityManager getEntityManager(String persistenceUnitName) { persistenceUnits.putIfAbsent(persistenceUnitName, Persistence.createEntityManagerFactory(persistenceUnitName)); return persistenceUnits.get(persistenceUnitName) .createEntityManager(); }  @SuppressWarnings("WeakerAccess") static synchronized EntityManager getEntityManager(String persistenceUnitName); static Session getSession(String persistenceUnitName); }
JPASessionUtil { @SuppressWarnings("WeakerAccess") public static synchronized EntityManager getEntityManager(String persistenceUnitName) { persistenceUnits.putIfAbsent(persistenceUnitName, Persistence.createEntityManagerFactory(persistenceUnitName)); return persistenceUnits.get(persistenceUnitName) .createEntityManager(); }  @SuppressWarnings("WeakerAccess") static synchronized EntityManager getEntityManager(String persistenceUnitName); static Session getSession(String persistenceUnitName);  }