Search in sources :

Example 6 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project OpenAttestation by OpenAttestation.

the class PersistenceManager method contextDestroyed.

@Override
public void contextDestroyed(ServletContextEvent sce) {
    for (String factoryName : factories.keySet()) {
        EntityManagerFactory factory = factories.get(factoryName);
        if (factory != null && factory.isOpen()) {
            log.info("PersistenceManager closing factory {} in contextDestroyed", factoryName);
            factory.close();
        }
        factories.remove(factoryName);
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory)

Example 7 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project OpenMEAP by OpenMEAP.

the class ModelTestUtils method resetTestDb.

public static void resetTestDb() {
    if (persistenceBeans != null) {
        EntityManagerFactory emf = getEntityManagerFactory();
        emf.close();
        persistenceBeans.close();
    }
    persistenceBeans = null;
    File testDbFile = new File(OPENMEAP_TEST_DB);
    if (testDbFile.exists() && !testDbFile.delete()) {
        throw new RuntimeException("Could not delete " + OPENMEAP_TEST_DB);
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) File(java.io.File)

Example 8 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project hibernate-orm by hibernate.

the class DdlInWildFlyUsingBmtAndEmfTest method testCreateThenDrop.

@Test
public void testCreateThenDrop() throws Exception {
    URL persistenceXmlUrl = Thread.currentThread().getContextClassLoader().getResource(PERSISTENCE_XML_RESOURCE_NAME);
    if (persistenceXmlUrl == null) {
        persistenceXmlUrl = Thread.currentThread().getContextClassLoader().getResource('/' + PERSISTENCE_XML_RESOURCE_NAME);
    }
    assertNotNull(persistenceXmlUrl);
    ParsedPersistenceXmlDescriptor persistenceUnit = PersistenceXmlParser.locateIndividualPersistenceUnit(persistenceXmlUrl);
    // creating the EMF causes SchemaCreator to be run...
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(persistenceUnit, Collections.emptyMap()).build();
    // closing the EMF causes the delayed SchemaDropper to be run...
    //		wrap in a transaction just to see if we can get this to fail in the way the WF report says;
    //		in my experience however this succeeds with or without the transaction
    final TransactionManager tm = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(JtaPlatform.class).retrieveTransactionManager();
    tm.begin();
    Transaction txn = tm.getTransaction();
    emf.close();
    txn.commit();
}
Also used : ParsedPersistenceXmlDescriptor(org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor) JBossAppServerJtaPlatform(org.hibernate.engine.transaction.jta.platform.internal.JBossAppServerJtaPlatform) JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) TransactionManager(javax.transaction.TransactionManager) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityManagerFactory(javax.persistence.EntityManagerFactory) URL(java.net.URL) Test(org.junit.Test)

Example 9 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class JpaTransactionManager method createEntityManagerForTransaction.

/**
	 * Create a JPA EntityManager to be used for a transaction.
	 * <p>The default implementation checks whether the EntityManagerFactory
	 * is a Spring proxy and unwraps it first.
	 * @see javax.persistence.EntityManagerFactory#createEntityManager()
	 * @see EntityManagerFactoryInfo#getNativeEntityManagerFactory()
	 */
protected EntityManager createEntityManagerForTransaction() {
    EntityManagerFactory emf = getEntityManagerFactory();
    if (emf instanceof EntityManagerFactoryInfo) {
        emf = ((EntityManagerFactoryInfo) emf).getNativeEntityManagerFactory();
    }
    Map<String, Object> properties = getJpaPropertyMap();
    return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager());
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory)

Example 10 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class EntityManagerFactoryAccessor method createEntityManager.

/**
	 * Obtain a new EntityManager from this accessor's EntityManagerFactory.
	 * <p>Can be overridden in subclasses to create specific EntityManager variants.
	 * @return a new EntityManager
	 * @throws IllegalStateException if this accessor is not configured with an EntityManagerFactory
	 * @see javax.persistence.EntityManagerFactory#createEntityManager()
	 * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
	 */
protected EntityManager createEntityManager() throws IllegalStateException {
    EntityManagerFactory emf = getEntityManagerFactory();
    Assert.state(emf != null, "No EntityManagerFactory specified");
    Map<String, Object> properties = getJpaPropertyMap();
    return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager());
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory)

Aggregations

EntityManagerFactory (javax.persistence.EntityManagerFactory)165 Test (org.junit.Test)72 EntityManager (javax.persistence.EntityManager)55 HashMap (java.util.HashMap)21 EJBException (javax.ejb.EJBException)19 AssertionFailedError (junit.framework.AssertionFailedError)17 TestFailureException (org.apache.openejb.test.TestFailureException)17 JMSException (javax.jms.JMSException)15 Map (java.util.Map)14 RemoteException (java.rmi.RemoteException)11 InitialContext (javax.naming.InitialContext)11 EntityTransaction (javax.persistence.EntityTransaction)10 PersistenceUnitDescriptorAdapter (org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter)10 NamingException (javax.naming.NamingException)8 PersistenceProvider (javax.persistence.spi.PersistenceProvider)7 BeanContext (org.apache.openejb.BeanContext)7 Item (org.hibernate.jpa.test.Item)7 Method (java.lang.reflect.Method)6 URL (java.net.URL)6 ArrayList (java.util.ArrayList)6