Search in sources :

Example 1 with Wallet

use of org.hibernate.jpa.test.Wallet 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 2 with Wallet

use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.

the class QueryTest method testPositionalParameterWithUserError.

@Test
public void testPositionalParameterWithUserError() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Wallet w = new Wallet();
        w.setBrand("Lacoste");
        w.setModel("Minimic");
        w.setSerial("0100202002");
        em.persist(w);
        em.flush();
        // using jpa-style, position index should match syntax '?<position'.
        Query jpaQuery = em.createQuery("select w from Wallet w where w.brand = ?1 and w.model = ?3");
        jpaQuery.setParameter(1, "Lacoste");
        try {
            jpaQuery.setParameter(2, "Expensive");
            fail("Should fail due to a user error in parameters");
        } catch (Exception e) {
            assertTyping(IllegalArgumentException.class, e);
        }
        // using jpa-style, position index specified not in query - test exception type
        jpaQuery = em.createQuery("select w from Wallet w ");
        try {
            Parameter parameter = jpaQuery.getParameter(1);
            fail("Should fail due to a user error in parameters");
        } catch (Exception e) {
            assertTyping(IllegalArgumentException.class, e);
        }
        // using jpa-style, position index specified not in query - test exception type
        jpaQuery = em.createQuery("select w from Wallet w");
        try {
            Parameter<Integer> parameter = jpaQuery.getParameter(1, Integer.class);
            fail("Should fail due to user error in parameters");
        } catch (Exception e) {
            assertTyping(IllegalArgumentException.class, e);
        }
        // using hql-style, should be 0-based
        Query hqlQuery = em.createQuery("select w from Wallet w where w.brand = ? and w.model = ?");
        try {
            hqlQuery.setParameter(1, "Lacoste");
            hqlQuery.setParameter(2, "Expensive");
            fail("Should fail due to a user error in parameters");
        } catch (Exception e) {
            assertTyping(IllegalArgumentException.class, e);
        }
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Wallet(org.hibernate.jpa.test.Wallet) Parameter(javax.persistence.Parameter) NoResultException(javax.persistence.NoResultException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Example 3 with Wallet

use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.

the class QueryTest method testNamedParameterWithUserError.

@Test
@TestForIssue(jiraKey = "HHH-10803")
public void testNamedParameterWithUserError() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Wallet w = new Wallet();
        w.setBrand("Lacoste");
        w.setModel("Minimic");
        w.setSerial("0100202002");
        em.persist(w);
        em.flush();
        Query jpaQuery = em.createQuery("select w from Wallet w");
        try {
            Parameter<?> parameter = jpaQuery.getParameter("brand");
            fail("Should fail due to user error in parameters");
        } catch (Exception e) {
            assertTyping(IllegalArgumentException.class, e);
        }
        jpaQuery = em.createQuery("select w from Wallet w");
        try {
            Parameter<String> parameter = jpaQuery.getParameter("brand", String.class);
            fail("Should fail due to user error in parameters");
        } catch (Exception e) {
            assertTyping(IllegalArgumentException.class, e);
        }
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Wallet(org.hibernate.jpa.test.Wallet) NoResultException(javax.persistence.NoResultException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with Wallet

use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.

the class DisableDiscardPersistenceContextOnCloseTest method testDiscardOnClose.

@Test
public void testDiscardOnClose() throws SQLException {
    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(1, connectionProvider.getAcquiredConnections().size());
        assertTrue(em.getTransaction().isActive());
    } finally {
        assertEquals(1, connectionProvider.getAcquiredConnections().size());
        em.getTransaction().rollback();
        assertEquals(0, connectionProvider.getAcquiredConnections().size());
        assertFalse(em.getTransaction().isActive());
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Wallet(org.hibernate.jpa.test.Wallet) Test(org.junit.Test)

Example 5 with Wallet

use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.

the class QueryTest method testNativeQuestionMarkParameter.

@Test
public void testNativeQuestionMarkParameter() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Wallet w = new Wallet();
        w.setBrand("Lacoste");
        w.setModel("Minimic");
        w.setSerial("0100202002");
        em.persist(w);
        em.getTransaction().commit();
        em.getTransaction().begin();
        Query query = em.createNativeQuery("select * from Wallet w where w.brand = ?", Wallet.class);
        query.setParameter(1, "Lacoste");
        w = (Wallet) query.getSingleResult();
        assertNotNull(w);
        em.remove(w);
        em.getTransaction().commit();
    } catch (Exception e) {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        throw e;
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Wallet(org.hibernate.jpa.test.Wallet) NoResultException(javax.persistence.NoResultException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)7 Wallet (org.hibernate.jpa.test.Wallet)7 Test (org.junit.Test)7 NoResultException (javax.persistence.NoResultException)5 PersistenceException (javax.persistence.PersistenceException)5 Query (javax.persistence.Query)5 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Parameter (javax.persistence.Parameter)1 TestForIssue (org.hibernate.testing.TestForIssue)1