Search in sources :

Example 96 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 97 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)

Example 98 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 99 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 100 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)

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