Search in sources :

Example 86 with EntityManager

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

the class EnableDiscardPersistenceContextOnCloseTest method testDiscardOnClose.

@Test
public void testDiscardOnClose() {
    EntityManager em = entityManagerFactory().createEntityManager();
    Wallet wallet = new Wallet();
    wallet.setSerial("123");
    try {
        em.getTransaction().begin();
        em.persist(wallet);
        assertEquals(1, connectionProvider.getAcquiredConnections().size());
        em.close();
        assertEquals(0, connectionProvider.getAcquiredConnections().size());
        assertTrue(em.getTransaction().isActive());
    } finally {
        try {
            em.getTransaction().rollback();
            fail("Should throw IllegalStateException because the Connection is already closed!");
        } catch (IllegalStateException expected) {
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Wallet(org.hibernate.jpa.test.Wallet) Test(org.junit.Test)

Example 87 with EntityManager

use of javax.persistence.EntityManager 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 88 with EntityManager

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

the class EntityManagerSerializationTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    EntityManager em = entityManagerFactory().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();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(stream);
    out.writeObject(em);
    out.close();
    byte[] serialized = stream.toByteArray();
    stream.close();
    ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
    ObjectInputStream 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) 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 89 with EntityManager

use of javax.persistence.EntityManager 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)

Example 90 with EntityManager

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

the class InterceptorTest method testDeprecatedConfiguredInterceptor.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test deprecated Interceptor settings
@Test
public void testDeprecatedConfiguredInterceptor() {
    Map settings = basicSettings();
    settings.put(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

EntityManager (javax.persistence.EntityManager)1435 Test (org.junit.Test)879 Priority (org.hibernate.envers.test.Priority)249 Query (javax.persistence.Query)211 TestForIssue (org.hibernate.testing.TestForIssue)97 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)87 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)79 List (java.util.List)75 NoResultException (javax.persistence.NoResultException)65 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)63 EntityManagerFactory (javax.persistence.EntityManagerFactory)59 HashMap (java.util.HashMap)50 StrTestEntity (org.hibernate.envers.test.entities.StrTestEntity)50 PersistenceException (javax.persistence.PersistenceException)43 ArrayList (java.util.ArrayList)41 EntityNotFoundException (javax.persistence.EntityNotFoundException)39 Transactional (com.google.inject.persist.Transactional)38 EntityTransaction (javax.persistence.EntityTransaction)38 Item (org.hibernate.jpa.test.Item)36 Session (org.hibernate.Session)35