Search in sources :

Example 71 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class QueryTest method testNativeQueryNullPositionalParameterParameter.

@Test
@TestForIssue(jiraKey = "HHH-10161")
@SkipForDialect(value = PostgreSQL9Dialect.class, jiraKey = "HHH-10312", comment = "Cannot convert untyped null (assumed to be bytea type) to bigint")
@SkipForDialect(value = PostgresPlusDialect.class, jiraKey = "HHH-10312", comment = "Cannot convert untyped null (assumed to be bytea type) to bigint")
@SkipForDialect(value = Oracle8iDialect.class, comment = "ORA-00932: inconsistent datatypes: expected NUMBER got BINARY")
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNativeQueryNullPositionalParameterParameter() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Item item = new Item("Mouse", "Micro$oft mouse");
        em.persist(item);
        // native queries don't seem to flush by default ?!?
        em.flush();
        Query q = em.createNativeQuery("select * from Item i where i.intVal=?");
        Parameter p = new Parameter() {

            @Override
            public String getName() {
                return null;
            }

            @Override
            public Integer getPosition() {
                return 1;
            }

            @Override
            public Class getParameterType() {
                return Integer.class;
            }
        };
        q.setParameter(p, null);
        Parameter pGotten = q.getParameter(p.getPosition());
        List results = q.getResultList();
        // null != null
        assertEquals(0, results.size());
        q = em.createNativeQuery("select * from Item i where i.intVal is null and ? is null");
        q.setParameter(p, null);
        results = q.getResultList();
        assertEquals(1, results.size());
        q = em.createNativeQuery("select * from Item i where i.intVal is null or i.intVal = ?");
        q.setParameter(p, null);
        results = q.getResultList();
        assertEquals(1, results.size());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Parameter(javax.persistence.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 72 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class QueryTest method testNativeQueryNullNamedParameterParameter.

@Test
@TestForIssue(jiraKey = "HHH-10161")
@SkipForDialect(value = PostgreSQL9Dialect.class, jiraKey = "HHH-10312", comment = "Cannot convert untyped null (assumed to be bytea type) to bigint")
@SkipForDialect(value = PostgresPlusDialect.class, jiraKey = "HHH-10312", comment = "Cannot convert untyped null (assumed to be bytea type) to bigint")
@SkipForDialect(value = Oracle8iDialect.class, comment = "ORA-00932: inconsistent datatypes: expected NUMBER got BINARY")
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNativeQueryNullNamedParameterParameter() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Item item = new Item("Mouse", "Micro$oft mouse");
        em.persist(item);
        // native queries don't seem to flush by default ?!?
        em.flush();
        Query q = em.createNativeQuery("select * from Item i where i.intVal=:iVal");
        Parameter p = new Parameter() {

            @Override
            public String getName() {
                return "iVal";
            }

            @Override
            public Integer getPosition() {
                return null;
            }

            @Override
            public Class getParameterType() {
                return Integer.class;
            }
        };
        q.setParameter(p, null);
        Parameter pGotten = q.getParameter(p.getName());
        List results = q.getResultList();
        assertEquals(0, results.size());
        q = em.createNativeQuery("select * from Item i where (i.intVal is null) and (:iVal is null)");
        q.setParameter(p, null);
        results = q.getResultList();
        assertEquals(1, results.size());
        q = em.createNativeQuery("select * from Item i where i.intVal is null or i.intVal = :iVal");
        q.setParameter(p, null);
        results = q.getResultList();
        assertEquals(1, results.size());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Parameter(javax.persistence.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 73 with TestForIssue

use of org.hibernate.testing.TestForIssue 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 74 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class TransactionJoiningTest method testIsJoinedAfterMarkedForRollbackExplicit.

@Test
@TestForIssue(jiraKey = "HHH-10807")
public void testIsJoinedAfterMarkedForRollbackExplicit() throws Exception {
    assertFalse(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    EntityManager entityManager = entityManagerFactory().createEntityManager(SynchronizationType.UNSYNCHRONIZED);
    SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
    assertTrue(entityManager.isOpen());
    assertTrue(session.isOpen());
    ExtraAssertions.assertTyping(JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator());
    JtaTransactionCoordinatorImpl transactionCoordinator = (JtaTransactionCoordinatorImpl) session.getTransactionCoordinator();
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    entityManager.joinTransaction();
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    transactionCoordinator.getTransactionDriverControl().markRollbackOnly();
    assertTrue(transactionCoordinator.isActive());
    assertTrue(transactionCoordinator.isJoined());
    assertTrue(entityManager.isJoinedToTransaction());
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
    entityManager.close();
    assertFalse(entityManager.isOpen());
    assertFalse(session.isOpen());
}
Also used : EntityManager(javax.persistence.EntityManager) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 75 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class TransactionJoiningTest method testMultiThreadTransactionTimeout.

/**
	 * In certain JTA environments (JBossTM, etc.), a background thread (reaper)
	 * can rollback a transaction if it times out.  These timeouts are rare and
	 * typically come from server failures.  However, we need to handle the
	 * multi-threaded nature of the transaction afterCompletion action.
	 * Emulate a timeout with a simple afterCompletion call in a thread.
	 * See HHH-7910
	 */
@Test
@TestForIssue(jiraKey = "HHH-7910")
public void testMultiThreadTransactionTimeout() throws Exception {
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager em = entityManagerFactory().createEntityManager();
    final SessionImpl sImpl = em.unwrap(SessionImpl.class);
    final CountDownLatch latch = new CountDownLatch(1);
    Thread thread = new Thread() {

        public void run() {
            ((JtaTransactionCoordinatorImpl) sImpl.getTransactionCoordinator()).getSynchronizationCallbackCoordinator().afterCompletion(Status.STATUS_ROLLEDBACK);
            latch.countDown();
        }
    };
    thread.start();
    latch.await();
    boolean caught = false;
    try {
        em.persist(new Book("The Book of Foo", 1));
    } catch (PersistenceException e) {
        caught = e.getCause().getClass().equals(HibernateException.class);
    }
    assertTrue(caught);
    // Ensure that the connection was closed by the background thread.
    caught = false;
    try {
        em.createQuery("from Book").getResultList();
    } catch (PersistenceException e) {
        // HHH-9312
        caught = true;
    } catch (Exception e) {
        caught = true;
    }
    assertTrue(caught);
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) SessionImpl(org.hibernate.internal.SessionImpl) CountDownLatch(java.util.concurrent.CountDownLatch) TransactionRequiredException(javax.persistence.TransactionRequiredException) PersistenceException(javax.persistence.PersistenceException) HibernateException(org.hibernate.HibernateException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

TestForIssue (org.hibernate.testing.TestForIssue)649 Test (org.junit.Test)647 Session (org.hibernate.Session)357 EntityManager (javax.persistence.EntityManager)97 List (java.util.List)91 Transaction (org.hibernate.Transaction)88 MetadataSources (org.hibernate.boot.MetadataSources)47 ArrayList (java.util.ArrayList)38 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)38 Query (org.hibernate.Query)28 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)25 Metadata (org.hibernate.boot.Metadata)24 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)24 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)23 Map (java.util.Map)22 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)19 HashMap (java.util.HashMap)18 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)18 PersistentClass (org.hibernate.mapping.PersistentClass)18 HibernateException (org.hibernate.HibernateException)16