Search in sources :

Example 76 with QueryException

use of org.hibernate.QueryException in project hibernate-orm by hibernate.

the class FooBarTest method testParameterCheck.

@Test
public void testParameterCheck() throws HibernateException {
    Session s = openSession();
    try {
        Query q = s.createQuery("select bar from Bar as bar where bar.x > :myX");
        q.list();
        fail("Should throw QueryException for missing myX");
    } catch (QueryException iae) {
    // should happen
    } finally {
        s.close();
    }
    s = openSession();
    try {
        Query q = s.createQuery("select bar from Bar as bar where bar.x > ?");
        q.list();
        fail("Should throw QueryException for missing ?");
    } catch (QueryException iae) {
    // should happen
    } finally {
        s.close();
    }
    s = openSession();
    try {
        Query q = s.createQuery("select bar from Bar as bar where bar.x > ? or bar.short = 1 or bar.string = 'ff ? bb'");
        q.setInteger(0, 1);
        q.list();
    } catch (QueryException iae) {
        fail("Should not throw QueryException for missing ?");
    } finally {
        s.close();
    }
    s = openSession();
    try {
        Query q = s.createQuery("select bar from Bar as bar where bar.string = ' ? ' or bar.string = '?'");
        q.list();
    } catch (QueryException iae) {
        fail("Should not throw QueryException for ? in quotes");
    } finally {
        s.close();
    }
    s = openSession();
    try {
        Query q = s.createQuery("select bar from Bar as bar where bar.string = ? or bar.string = ? or bar.string = ?");
        q.setParameter(0, "bull");
        q.setParameter(2, "shit");
        q.list();
        fail("should throw exception telling me i have not set parameter 1");
    } catch (QueryException iae) {
    // should happen!
    } finally {
        s.close();
    }
}
Also used : QueryException(org.hibernate.QueryException) Query(org.hibernate.Query) Session(org.hibernate.Session) Test(org.junit.Test)

Example 77 with QueryException

use of org.hibernate.QueryException in project ACS by ACS-Community.

the class TestPojosPersistence method testHQL.

public void testHQL() throws Exception {
    createDB();
    try {
        createConfigurationComputerAndTwoNetworkDevices();
        Configuration config = (Configuration) hibernateUtil.getList(Configuration.class).iterator().next();
        assertNotNull(config);
        // Now we test that using HQL queries
        Query q = hibernateUtil.getSession().createQuery("from NetworkDevice as nd where nd.name = ?");
        q.setParameter(0, "wall-e");
        assertEquals(2, q.list().size());
        q = hibernateUtil.getSession().createQuery("from NetworkDevice as nd where nd.name = ? and nd.networkName = ?");
        q.setParameter(0, "wall-e");
        q.setParameter(1, "wall-e.eso.org");
        assertEquals(1, q.list().size());
        q = hibernateUtil.getSession().createQuery("from NetworkDevice as nd where nd.configuration = ?");
        q.setParameter(0, config);
        assertEquals(3, q.list().size());
        q = hibernateUtil.getSession().createQuery("from Configuration as conf where conf.configurationName = ? and creationTime < ?");
        q.setParameter(0, "rtobarConfig");
        q.setParameter(1, new Date());
        assertEquals(1, q.list().size());
        try {
            // typo: should be configurationName
            q = hibernateUtil.getSession().createQuery("from Configuration as conf where conf.configuratioName = ?");
            q.setParameter(0, "rtobarConfig");
            q.list();
            fail("Should fail, property 'configuratioName' doesn't exist for Configuration objects");
        } catch (QueryException e) {
        }
    } finally {
        dropDB();
    }
}
Also used : QueryException(org.hibernate.QueryException) Query(org.hibernate.Query) Date(java.util.Date)

Aggregations

QueryException (org.hibernate.QueryException)77 Type (org.hibernate.type.Type)20 Test (org.junit.Test)19 Session (org.hibernate.Session)18 Transaction (org.hibernate.Transaction)12 JoinType (org.hibernate.sql.JoinType)12 Queryable (org.hibernate.persister.entity.Queryable)10 CollectionType (org.hibernate.type.CollectionType)10 MappingException (org.hibernate.MappingException)9 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)9 AssociationType (org.hibernate.type.AssociationType)8 HashMap (java.util.HashMap)6 HibernateException (org.hibernate.HibernateException)6 JoinSequence (org.hibernate.engine.internal.JoinSequence)6 EntityType (org.hibernate.type.EntityType)6 AST (antlr.collections.AST)5 Map (java.util.Map)5 SemanticException (antlr.SemanticException)4 ArrayList (java.util.ArrayList)4 FromElement (org.hibernate.hql.internal.ast.tree.FromElement)4