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();
}
}
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();
}
}
Aggregations