List of usage examples for org.hibernate QueryException QueryException
public QueryException(String message, String queryString)
From source file:org.brushingbits.jnap.persistence.hibernate.DynaQueryBuilder.java
License:Apache License
public Criteria build() throws QueryException { Matcher matcher = DYNA_QUERY_PATTERN.matcher(this.dynaQuery); if (!matcher.matches()) { throw new QueryException("The DynaQuery syntax is incorrect. It must start with 'findBy' " + ", findUniqueBy or 'countBy' expression followed by property expressions and operators.", this.dynaQuery); }/*ww w . ja va2 s . co m*/ Criteria criteria = this.createCriteria(matcher.group(1).equals("countBy")); String dynaQueryExpression = matcher.group(2); // order by Matcher orderByMatcher = ORDER_BY_PATTERN.matcher(dynaQueryExpression); if (orderByMatcher.find()) { dynaQueryExpression = StringUtils.remove(dynaQueryExpression, orderByMatcher.group()); String orderByProperty = normalizePropertyName(orderByMatcher.group(3)); String orderByDirection = orderByMatcher.group(4); Order orderBy = "Desc".equals(orderByDirection) ? Order.desc(orderByProperty) : Order.asc(orderByProperty); criteria.addOrder(orderBy); } // split properties String[] properties = DYNA_QUERY_OPERATOR_PATTERN.split(dynaQueryExpression); if (properties.length == 0 || properties.length > 2) { throw new QueryException(format( "The DynaQuery syntax is incorrect. Dynamic queries must have " + "at least one property an no more than two (yours has {0}).\nYou can use one of " + "the following logical operators ({1}) and these expression operators ({2})", properties.length, LOGICAL_OPERATOR_AND + ", " + LOGICAL_OPERATOR_OR, StringUtils.join(EXPRESSION_OPERATORS, ", ")), this.dynaQuery); } Matcher logicalOperatorsMatcher = DYNA_QUERY_OPERATOR_PATTERN.matcher(dynaQueryExpression); String logicalOperator = logicalOperatorsMatcher.find() ? logicalOperatorsMatcher.group(1) : LOGICAL_OPERATOR_AND; Junction junction = LOGICAL_OPERATOR_OR.equals(logicalOperator) ? Restrictions.disjunction() : Restrictions.conjunction(); for (String property : properties) { junction.add(this.createCriterion(property)); } criteria.add(junction); return criteria; }
From source file:org.brushingbits.jnap.persistence.hibernate.DynaQueryBuilder.java
License:Apache License
Object[] getPropertyValues(int size) { if ((propertyValueIndex + size) <= this.queryParams.length) { Object[] values = ArrayUtils.subarray(this.queryParams, propertyValueIndex, propertyValueIndex + size); propertyValueIndex += size;// www . j a v a 2s .c o m return values; } else { throw new QueryException( format("The number of parameters ({0}) does not satisfy " + "DynaQuery expression", Integer.toString(this.queryParams.length)), this.dynaQuery); } }
From source file:org.brushingbits.jnap.persistence.hibernate.DynaQueryBuilder.java
License:Apache License
void validateQueryProperty(String propertyName, Object value) { if (!ArrayUtils.contains(this.entityMetadata.getPropertyNames(), propertyName)) { throw new QueryException( format("Error creating a DynaQuery, the property {0} is not " + "mapped for entity {1}.", propertyName, this.entityName), this.dynaQuery); }/*w ww . j a v a 2s. c om*/ if (value != null) { Class propertyType = this.entityMetadata.getPropertyType(propertyName).getReturnedClass(); if (!propertyType.isAssignableFrom(value.getClass())) { throw new QueryException(format( "Error creating a DynaQuery, the property {0} type " + "({1}) is not compatible with the argument type ({2}).", propertyName, propertyType.getName(), value.getClass().getName()), this.dynaQuery); } } }
From source file:org.jnap.core.persistence.hibernate.DynaQueryBuilder.java
License:Apache License
public Criteria build() throws QueryException { Matcher matcher = DYNA_QUERY_PATTERN.matcher(this.dynaQuery); if (!matcher.matches()) { throw new QueryException("The QueryMethod syntax is incorrect. It must start with 'findBy' " + ", findUniqueBy or 'countBy' expression followed by property expressions and operators.", this.dynaQuery); }/*from w w w . ja v a2 s. c om*/ Criteria criteria = this.createCriteria(matcher.group(1).equals("countBy")); String dynaQueryExpression = matcher.group(2); // order by Matcher orderByMatcher = ORDER_BY_PATTERN.matcher(dynaQueryExpression); if (orderByMatcher.find()) { dynaQueryExpression = StringUtils.remove(dynaQueryExpression, orderByMatcher.group()); String orderByProperty = normalizePropertyName(orderByMatcher.group(3)); String orderByDirection = orderByMatcher.group(4); Order orderBy = "Desc".equals(orderByDirection) ? Order.desc(orderByProperty) : Order.asc(orderByProperty); criteria.addOrder(orderBy); } // split properties String[] properties = DYNA_QUERY_OPERATOR_PATTERN.split(dynaQueryExpression); if (properties.length == 0 || properties.length > 2) { throw new QueryException(format( "The QueryMethod syntax is incorrect. Dynamic queries must have " + "at least one property an no more than two (yours has {0}).\nYou can use one of " + "the following logical operators ({1}) and these expression operators ({2})", properties.length, LOGICAL_OPERATOR_AND + ", " + LOGICAL_OPERATOR_OR, StringUtils.join(EXPRESSION_OPERATORS, ", ")), this.dynaQuery); } Matcher logicalOperatorsMatcher = DYNA_QUERY_OPERATOR_PATTERN.matcher(dynaQueryExpression); String logicalOperator = logicalOperatorsMatcher.find() ? logicalOperatorsMatcher.group(1) : LOGICAL_OPERATOR_AND; Junction junction = LOGICAL_OPERATOR_OR.equals(logicalOperator) ? Restrictions.disjunction() : Restrictions.conjunction(); for (String property : properties) { junction.add(this.createCriterion(property)); } criteria.add(junction); return criteria; }
From source file:org.jnap.core.persistence.hibernate.DynaQueryBuilder.java
License:Apache License
Object[] getPropertyValues(int size) { if ((propertyValueIndex + size) <= this.queryParams.length) { Object[] values = ArrayUtils.subarray(this.queryParams, propertyValueIndex, propertyValueIndex + size); propertyValueIndex += size;//from ww w . ja v a 2 s .co m return values; } else { throw new QueryException( format("The number of parameters ({0}) does not satisfy " + "QueryMethod expression", Integer.toString(this.queryParams.length)), this.dynaQuery); } }
From source file:org.jnap.core.persistence.hibernate.DynaQueryBuilder.java
License:Apache License
void validateQueryProperty(String propertyName, Object value) { if (!ArrayUtils.contains(this.entityMetadata.getPropertyNames(), propertyName)) { throw new QueryException( format("Error creating a QueryMethod, the property {0} is not " + "mapped for entity {1}.", propertyName, this.entityName), this.dynaQuery); }/*from www.jav a2s. com*/ if (value != null) { Class propertyType = this.entityMetadata.getPropertyType(propertyName).getReturnedClass(); if (!propertyType.isAssignableFrom(value.getClass())) { throw new QueryException(format( "Error creating a QueryMethod, the property {0} type " + "({1}) is not compatible with the argument type ({2}).", propertyName, propertyType.getName(), value.getClass().getName()), this.dynaQuery); } } }
From source file:org.springframework.orm.hibernate4.HibernateTemplateTests.java
License:Apache License
@Test public void testExceptions() { SQLException sqlEx = new SQLException("argh", "27"); final JDBCConnectionException jcex = new JDBCConnectionException("mymsg", sqlEx); try {/*from w w w.j ava 2s .c om*/ hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw jcex; } }); fail("Should have thrown DataAccessResourceFailureException"); } catch (DataAccessResourceFailureException ex) { // expected assertEquals(jcex, ex.getCause()); assertTrue(ex.getMessage().contains("mymsg")); } final SQLGrammarException sgex = new SQLGrammarException("mymsg", sqlEx); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw sgex; } }); fail("Should have thrown InvalidDataAccessResourceUsageException"); } catch (InvalidDataAccessResourceUsageException ex) { // expected assertEquals(sgex, ex.getCause()); assertTrue(ex.getMessage().contains("mymsg")); } final LockAcquisitionException laex = new LockAcquisitionException("mymsg", sqlEx); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw laex; } }); fail("Should have thrown CannotAcquireLockException"); } catch (CannotAcquireLockException ex) { // expected assertEquals(laex, ex.getCause()); assertTrue(ex.getMessage().contains("mymsg")); } final ConstraintViolationException cvex = new ConstraintViolationException("mymsg", sqlEx, "myconstraint"); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw cvex; } }); fail("Should have thrown DataIntegrityViolationException"); } catch (DataIntegrityViolationException ex) { // expected assertEquals(cvex, ex.getCause()); assertTrue(ex.getMessage().contains("mymsg")); } final DataException dex = new DataException("mymsg", sqlEx); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw dex; } }); fail("Should have thrown DataIntegrityViolationException"); } catch (DataIntegrityViolationException ex) { // expected assertEquals(dex, ex.getCause()); assertTrue(ex.getMessage().contains("mymsg")); } final JDBCException jdex = new JDBCException("mymsg", sqlEx); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw jdex; } }); fail("Should have thrown HibernateJdbcException"); } catch (HibernateJdbcException ex) { // expected assertEquals(jdex, ex.getCause()); assertTrue(ex.getMessage().contains("mymsg")); } final PropertyValueException pvex = new PropertyValueException("mymsg", "myentity", "myproperty"); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw pvex; } }); fail("Should have thrown DataIntegrityViolationException"); } catch (DataIntegrityViolationException ex) { // expected assertEquals(pvex, ex.getCause()); assertTrue(ex.getMessage().contains("mymsg")); } try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw new PersistentObjectException(""); } }); fail("Should have thrown InvalidDataAccessApiUsageException"); } catch (InvalidDataAccessApiUsageException ex) { // expected } try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw new TransientObjectException(""); } }); fail("Should have thrown InvalidDataAccessApiUsageException"); } catch (InvalidDataAccessApiUsageException ex) { // expected } final ObjectDeletedException odex = new ObjectDeletedException("msg", "id", TestBean.class.getName()); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw odex; } }); fail("Should have thrown InvalidDataAccessApiUsageException"); } catch (InvalidDataAccessApiUsageException ex) { // expected assertEquals(odex, ex.getCause()); } final QueryException qex = new QueryException("msg", "query"); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw qex; } }); fail("Should have thrown InvalidDataAccessResourceUsageException"); } catch (HibernateQueryException ex) { // expected assertEquals(qex, ex.getCause()); assertEquals("query", ex.getQueryString()); } final UnresolvableObjectException uoex = new UnresolvableObjectException("id", TestBean.class.getName()); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw uoex; } }); fail("Should have thrown HibernateObjectRetrievalFailureException"); } catch (HibernateObjectRetrievalFailureException ex) { // expected assertEquals(TestBean.class.getName(), ex.getPersistentClassName()); assertEquals("id", ex.getIdentifier()); assertEquals(uoex, ex.getCause()); } final ObjectNotFoundException onfe = new ObjectNotFoundException("id", TestBean.class.getName()); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw onfe; } }); fail("Should have thrown HibernateObjectRetrievalFailureException"); } catch (HibernateObjectRetrievalFailureException ex) { // expected assertEquals(TestBean.class.getName(), ex.getPersistentClassName()); assertEquals("id", ex.getIdentifier()); assertEquals(onfe, ex.getCause()); } final WrongClassException wcex = new WrongClassException("msg", "id", TestBean.class.getName()); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw wcex; } }); fail("Should have thrown HibernateObjectRetrievalFailureException"); } catch (HibernateObjectRetrievalFailureException ex) { // expected assertEquals(TestBean.class.getName(), ex.getPersistentClassName()); assertEquals("id", ex.getIdentifier()); assertEquals(wcex, ex.getCause()); } final NonUniqueResultException nuex = new NonUniqueResultException(2); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw nuex; } }); fail("Should have thrown IncorrectResultSizeDataAccessException"); } catch (IncorrectResultSizeDataAccessException ex) { // expected assertEquals(1, ex.getExpectedSize()); assertEquals(-1, ex.getActualSize()); } final StaleObjectStateException sosex = new StaleObjectStateException(TestBean.class.getName(), "id"); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw sosex; } }); fail("Should have thrown HibernateOptimisticLockingFailureException"); } catch (HibernateOptimisticLockingFailureException ex) { // expected assertEquals(TestBean.class.getName(), ex.getPersistentClassName()); assertEquals("id", ex.getIdentifier()); assertEquals(sosex, ex.getCause()); } final StaleStateException ssex = new StaleStateException("msg"); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw ssex; } }); fail("Should have thrown HibernateOptimisticLockingFailureException"); } catch (HibernateOptimisticLockingFailureException ex) { // expected assertNull(ex.getPersistentClassName()); assertNull(ex.getIdentifier()); assertEquals(ssex, ex.getCause()); } final HibernateException hex = new HibernateException("msg"); try { hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) { throw hex; } }); fail("Should have thrown HibernateSystemException"); } catch (HibernateSystemException ex) { // expected assertEquals(hex, ex.getCause()); } }