Search in sources :

Example 61 with EntityManagerFactory

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

the class SessionFactoryObserverTest method testSessionFactoryObserverProperty.

@Test
public void testSessionFactoryObserverProperty() {
    EntityManagerFactoryBuilder builder = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitInfoAdapter(), Collections.singletonMap(AvailableSettings.SESSION_FACTORY_OBSERVER, GoofySessionFactoryObserver.class.getName()));
    try {
        final EntityManagerFactory entityManagerFactory = builder.build();
        entityManagerFactory.close();
        Assert.fail("GoofyException should have been thrown");
    } catch (GoofyException e) {
    // success
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) EntityManagerFactoryBuilder(org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder) PersistenceUnitInfoAdapter(org.hibernate.jpa.test.PersistenceUnitInfoAdapter) Test(org.junit.Test)

Example 62 with EntityManagerFactory

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

the class EntityManagerFactorySerializationTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(stream);
    out.writeObject(entityManagerFactory());
    out.close();
    byte[] serialized = stream.toByteArray();
    stream.close();
    ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
    ObjectInputStream in = new ObjectInputStream(byteIn);
    EntityManagerFactory serializedFactory = (EntityManagerFactory) in.readObject();
    in.close();
    byteIn.close();
    EntityManager em = serializedFactory.createEntityManager();
    // em.getTransaction().begin();
    // em.setFlushMode( FlushModeType.NEVER );
    Cat cat = new Cat();
    cat.setAge(3);
    cat.setDateOfBirth(new Date());
    cat.setLength(22);
    cat.setName("Kitty");
    em.persist(cat);
    Item item = new Item();
    item.setName("Train Ticket");
    item.setDescr("Paris-London");
    em.persist(item);
    // em.getTransaction().commit();
    // em.getTransaction().begin();
    item.setDescr("Paris-Bruxelles");
    // em.getTransaction().commit();
    // fake the in container work
    ((HibernateEntityManager) em).getSession().disconnect();
    stream = new ByteArrayOutputStream();
    out = new ObjectOutputStream(stream);
    out.writeObject(em);
    out.close();
    serialized = stream.toByteArray();
    stream.close();
    byteIn = new ByteArrayInputStream(serialized);
    in = new ObjectInputStream(byteIn);
    em = (EntityManager) in.readObject();
    in.close();
    byteIn.close();
    // fake the in container work
    em.getTransaction().begin();
    item = em.find(Item.class, item.getName());
    item.setDescr(item.getDescr() + "-Amsterdam");
    cat = (Cat) em.createQuery("select c from " + Cat.class.getName() + " c").getSingleResult();
    cat.setLength(34);
    em.flush();
    em.remove(item);
    em.remove(cat);
    em.flush();
    em.getTransaction().commit();
    em.close();
}
Also used : Item(org.hibernate.jpa.test.Item) HibernateEntityManager(org.hibernate.jpa.HibernateEntityManager) EntityManager(javax.persistence.EntityManager) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityManagerFactory(javax.persistence.EntityManagerFactory) Cat(org.hibernate.jpa.test.Cat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Date(java.util.Date) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 63 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)

Example 64 with EntityManagerFactory

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

the class NewBootProcessTest method basicNewBootProcessTest.

@Test
public void basicNewBootProcessTest() {
    Map settings = new HashMap();
    HibernatePersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
    final EntityManagerFactory emf = persistenceProvider.createContainerEntityManagerFactory(new JpaXsdVersionsTest.PersistenceUnitInfoImpl("my-test") {

        @Override
        public URL getPersistenceUnitRootUrl() {
            // just get any known url...
            return HibernatePersistenceProvider.class.getResource("/org/hibernate/jpa/persistence_1_0.xsd");
        }
    }, settings);
    emf.close();
}
Also used : JpaXsdVersionsTest(org.hibernate.test.jpa.xml.versions.JpaXsdVersionsTest) HashMap(java.util.HashMap) EntityManagerFactory(javax.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) Map(java.util.Map) HashMap(java.util.HashMap) URL(java.net.URL) JpaXsdVersionsTest(org.hibernate.test.jpa.xml.versions.JpaXsdVersionsTest) Test(org.junit.Test)

Example 65 with EntityManagerFactory

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

the class EntityTypeChangeAuditTrackingRevisionListenerTest method testLifecycle.

@Test
public void testLifecycle() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Customer customer = new Customer();
        customer.setId(1L);
        customer.setFirstName("John");
        customer.setLastName("Doe");
        entityManager.persist(customer);
    });
    EntityManagerFactory entityManagerFactory = null;
    try {
        Map settings = buildSettings();
        settings.put(org.hibernate.jpa.AvailableSettings.LOADED_CLASSES, Arrays.asList(ApplicationCustomer.class, CustomTrackingRevisionEntity.class, EntityType.class));
        settings.put(AvailableSettings.HBM2DDL_AUTO, "update");
        entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(new TestingPersistenceUnitDescriptorImpl(getClass().getSimpleName()), settings).build().unwrap(SessionFactoryImplementor.class);
        final EntityManagerFactory emf = entityManagerFactory;
        doInJPA(() -> emf, entityManager -> {
            ApplicationCustomer customer = new ApplicationCustomer();
            customer.setId(2L);
            customer.setFirstName("John");
            customer.setLastName("Doe Jr.");
            entityManager.persist(customer);
        });
        doInJPA(() -> emf, entityManager -> {
            // tag::envers-tracking-modified-entities-revchanges-query-example[]
            AuditReader auditReader = AuditReaderFactory.get(entityManager);
            List<Number> revisions = auditReader.getRevisions(ApplicationCustomer.class, 1L);
            CustomTrackingRevisionEntity revEntity = auditReader.findRevision(CustomTrackingRevisionEntity.class, revisions.get(0));
            Set<EntityType> modifiedEntityTypes = revEntity.getModifiedEntityTypes();
            assertEquals(1, modifiedEntityTypes.size());
            EntityType entityType = modifiedEntityTypes.iterator().next();
            assertEquals(Customer.class.getName(), entityType.getEntityClassName());
        // end::envers-tracking-modified-entities-revchanges-query-example[]
        });
    } finally {
        if (entityManagerFactory != null) {
            entityManagerFactory.close();
        }
    }
}
Also used : RevisionNumber(org.hibernate.envers.RevisionNumber) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityManagerFactory(javax.persistence.EntityManagerFactory) AuditReader(org.hibernate.envers.AuditReader) Map(java.util.Map) Test(org.junit.Test)

Aggregations

EntityManagerFactory (javax.persistence.EntityManagerFactory)302 EntityManager (javax.persistence.EntityManager)103 Test (org.junit.Test)90 HashMap (java.util.HashMap)48 EntityTransaction (javax.persistence.EntityTransaction)30 EJBException (javax.ejb.EJBException)22 Map (java.util.Map)17 AssertionFailedError (junit.framework.AssertionFailedError)17 TestFailureException (org.apache.openejb.test.TestFailureException)17 ArrayList (java.util.ArrayList)15 JMSException (javax.jms.JMSException)15 KieSession (org.kie.api.runtime.KieSession)14 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)14 Properties (java.util.Properties)13 InitialContext (javax.naming.InitialContext)13 List (java.util.List)12 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)12 RemoteException (java.rmi.RemoteException)11 Query (javax.persistence.Query)11 PrintWriter (java.io.PrintWriter)10