Search in sources :

Example 21 with EntityManagerFactory

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

the class SharedEntityManagerCreatorTests method transactionRequiredExceptionOnFlush.

@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnFlush() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    em.flush();
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) Test(org.junit.Test)

Example 22 with EntityManagerFactory

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

the class SharedEntityManagerCreatorTests method transactionRequiredExceptionOnPersist.

@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnPersist() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    em.persist(new Object());
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) Test(org.junit.Test)

Example 23 with EntityManagerFactory

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

the class SharedEntityManagerCreatorTests method transactionRequiredExceptionOnMerge.

@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnMerge() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    em.merge(new Object());
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) Test(org.junit.Test)

Example 24 with EntityManagerFactory

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

the class ListenerTest method testLoadListener.

@Test(expected = SecurityException.class)
public void testLoadListener() {
    Serializable customerId = 1L;
    doInJPA(this::entityManagerFactory, entityManager -> {
        EntityManagerFactory entityManagerFactory = entityManagerFactory();
        SessionFactoryImplementor sessionFactory = entityManagerFactory.unwrap(SessionFactoryImplementor.class);
        sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class).prependListeners(EventType.LOAD, new SecuredLoadEntityListener());
        Customer customer = entityManager.find(Customer.class, customerId);
    });
}
Also used : Serializable(java.io.Serializable) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityManagerFactory(javax.persistence.EntityManagerFactory) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) Test(org.junit.Test)

Example 25 with EntityManagerFactory

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

the class EntityManagerFactoryClosedTest method testWithTransactionalEnvironment.

/**
	 * Test that using a closed EntityManagerFactory throws an IllegalStateException
	 * Also ensure that HHH-8586 doesn't regress.
	 * @throws Exception
	 */
@Test
public void testWithTransactionalEnvironment() throws Exception {
    assertFalse(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    EntityManagerFactory entityManagerFactory = entityManagerFactory();
    // close the underlying entity manager factory
    entityManagerFactory.close();
    try {
        entityManagerFactory.createEntityManager();
        fail("expected IllegalStateException when calling emf.createEntityManager with closed emf");
    } catch (IllegalStateException expected) {
    // success
    }
    try {
        entityManagerFactory.getCriteriaBuilder();
        fail("expected IllegalStateException when calling emf.getCriteriaBuilder with closed emf");
    } catch (IllegalStateException expected) {
    // success
    }
    try {
        entityManagerFactory.getCache();
        fail("expected IllegalStateException when calling emf.getCache with closed emf");
    } catch (IllegalStateException expected) {
    // success
    }
    assertFalse(entityManagerFactory.isOpen());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) Test(org.junit.Test)

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