Search in sources :

Example 66 with TestForIssue

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

the class QueryTest method testCacheStoreAndRetrieveModeParameter.

@Test
@TestForIssue(jiraKey = "HHH_8949")
public void testCacheStoreAndRetrieveModeParameter() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Query query = em.createQuery("select item from Item item");
        query.getHints().clear();
        query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE);
        query.setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
        assertEquals(CacheRetrieveMode.USE, query.getHints().get("javax.persistence.cache.retrieveMode"));
        assertEquals(CacheStoreMode.REFRESH, query.getHints().get("javax.persistence.cache.storeMode"));
        query.getHints().clear();
        query.setHint("javax.persistence.cache.retrieveMode", "USE");
        query.setHint("javax.persistence.cache.storeMode", "REFRESH");
        assertEquals(CacheRetrieveMode.USE, query.getHints().get("javax.persistence.cache.retrieveMode"));
        assertEquals(CacheStoreMode.REFRESH, query.getHints().get("javax.persistence.cache.storeMode"));
        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) NoResultException(javax.persistence.NoResultException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 67 with TestForIssue

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

the class QueryTest method testGetSingleResultWithNoResultException.

@Test
@TestForIssue(jiraKey = "HHH-10833")
public void testGetSingleResultWithNoResultException() {
    final EntityManager entityManager = getOrCreateEntityManager();
    try {
        entityManager.createQuery("FROM Item WHERE name = 'bozo'").getSingleResult();
        fail("Expected NoResultException");
    } catch (Exception e) {
        assertTyping(NoResultException.class, e);
    } finally {
        entityManager.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) NoResultException(javax.persistence.NoResultException) NoResultException(javax.persistence.NoResultException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 68 with TestForIssue

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

the class NativeQueryOrdinalParametersTest method testConflictWithSessionNativeQuery.

@Test
@TestForIssue(jiraKey = "HHH-11121")
public void testConflictWithSessionNativeQuery() {
    EntityManager em = getOrCreateEntityManager();
    final String sqlString = "SELECT * FROM GAME g WHERE title = ?";
    try {
        NativeQuery sqlQuery = em.unwrap(Session.class).createSQLQuery(sqlString);
        sqlQuery.setString(0, "Super Mario Brothers").setCacheable(true);
        List results = sqlQuery.list();
        assertEquals(1, results.size());
        NativeQueryImplementor query = (NativeQueryImplementor) em.createNativeQuery(sqlString);
        query.setString(1, "Super Mario Brothers");
        List list = query.list();
        assertEquals(1, list.size());
        sqlQuery = em.unwrap(Session.class).createSQLQuery(sqlString);
        sqlQuery.setString(0, "Super Mario Brothers").setCacheable(true);
        results = sqlQuery.list();
        assertEquals(1, results.size());
        query.setString(1, "Super Mario Brothers");
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) NativeQueryImplementor(org.hibernate.query.spi.NativeQueryImplementor) NativeQuery(org.hibernate.query.NativeQuery) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 69 with TestForIssue

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

the class NativeQueryOrdinalParametersTest method testNativeQueryOrdinalParameter.

@Test
@TestForIssue(jiraKey = "HHH-10885")
public void testNativeQueryOrdinalParameter() {
    EntityManager em = getOrCreateEntityManager();
    try {
        Query query = em.createNativeQuery("SELECT * FROM GAME g WHERE title = ?");
        query.setParameter(1, "Super Mario Brothers");
        List list = query.getResultList();
        assertEquals(1, list.size());
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NativeQuery(org.hibernate.query.NativeQuery) List(java.util.List) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 70 with TestForIssue

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

the class NativeQueryOrdinalParametersTest method testNativeQueryIndexedOrdinalParameter.

@Test
@TestForIssue(jiraKey = "HHH-10885")
public void testNativeQueryIndexedOrdinalParameter() {
    EntityManager em = getOrCreateEntityManager();
    try {
        Query query = em.createNativeQuery("SELECT * FROM GAME g WHERE title = ?1");
        query.setParameter(1, "Super Mario Brothers");
        List list = query.getResultList();
        assertEquals(1, list.size());
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NativeQuery(org.hibernate.query.NativeQuery) List(java.util.List) 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