Search in sources :

Example 26 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 OrmVersionTest.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 : HashMap(java.util.HashMap) EntityManagerFactory(javax.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) OrmVersionTest(org.hibernate.jpa.test.jee.OrmVersionTest) Map(java.util.Map) HashMap(java.util.HashMap) URL(java.net.URL) OrmVersionTest(org.hibernate.jpa.test.jee.OrmVersionTest) Test(org.junit.Test)

Example 27 with EntityManagerFactory

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

the class NoCdiAvailableTest method testJpaBootstrapWithoutCdiAvailable.

@Test
public void testJpaBootstrapWithoutCdiAvailable() throws Exception {
    Class delegateClass = Thread.currentThread().getContextClassLoader().loadClass("org.hibernate.jpa.test.cdi.NoCdiAvailableTestDelegate");
    Method mainMethod = delegateClass.getMethod("passingNoBeanManager");
    EntityManagerFactory entityManagerFactory = null;
    try {
        entityManagerFactory = (EntityManagerFactory) mainMethod.invoke(null);
    } finally {
        if (entityManagerFactory != null) {
            entityManagerFactory.close();
        }
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 28 with EntityManagerFactory

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

the class EntityManagerFactorySerializationTest method testEntityManagerFactoryProperties.

@Test
public void testEntityManagerFactoryProperties() {
    EntityManagerFactory entityManagerFactory = entityManagerFactory();
    assertTrue(entityManagerFactory.getProperties().containsKey(AvailableSettings.USER));
    if (entityManagerFactory.getProperties().containsKey(AvailableSettings.PASS)) {
        assertEquals("****", entityManagerFactory.getProperties().get(AvailableSettings.PASS));
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) Test(org.junit.Test)

Example 29 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 30 with EntityManagerFactory

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

the class InterceptorTest method testConfiguredInterceptor.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test Interceptor settings
@Test
public void testConfiguredInterceptor() {
    Map settings = basicSettings();
    settings.put(org.hibernate.cfg.AvailableSettings.INTERCEPTOR, ExceptionInterceptor.class.getName());
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
    EntityManager em = emf.createEntityManager();
    Item i = new Item();
    i.setName("Laptop");
    try {
        em.getTransaction().begin();
        em.persist(i);
        em.getTransaction().commit();
        fail("No interceptor");
    } catch (IllegalStateException e) {
        assertEquals(ExceptionInterceptor.EXCEPTION_MESSAGE, e.getMessage());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
        emf.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) Map(java.util.Map) 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