List of usage examples for org.springframework.dao InvalidDataAccessApiUsageException InvalidDataAccessApiUsageException
public InvalidDataAccessApiUsageException(String msg)
From source file:org.springframework.data.redis.listener.adapter.MessageListenerAdapter.java
public void afterPropertiesSet() { String methodName = getDefaultListenerMethod(); if (!StringUtils.hasText(methodName)) { throw new InvalidDataAccessApiUsageException("No default listener method specified: " + "Either specify a non-null value for the 'defaultListenerMethod' property or " + "override the 'getListenerMethodName' method."); }//from w w w.j a va2 s .c om invoker = new MethodInvoker(delegate, methodName); }
From source file:org.springframework.data.solr.core.QueryParser.java
private void appendGroupByFields(SolrQuery solrQuery, List<Field> fields) { if (CollectionUtils.isEmpty(fields)) { return;//from w w w. j a va2 s . c o m } if (fields.size() > 1) { // there is a bug in solj which prevents multiple grouping // although available via HTTP call throw new InvalidDataAccessApiUsageException( "Cannot group on more than one field with current SolrJ API. Group on single field insead"); } solrQuery.set(GroupParams.GROUP, true); solrQuery.setParam(GroupParams.GROUP_MAIN, true); for (Field field : fields) { solrQuery.add(GroupParams.GROUP_FIELD, field.getName()); } }
From source file:org.springframework.datastore.mapping.jpa.query.JpaQuery.java
public JpaQuery(JpaSession session, PersistentEntity entity) { super(session, entity); if (session == null) { throw new InvalidDataAccessApiUsageException("Argument session cannot be null"); }/* w ww.j a va 2 s . c om*/ if (entity == null) { throw new InvalidDataAccessApiUsageException("No persistent entity specified"); } }
From source file:org.springframework.jdbc.core.AbstractBeanPropertyRowMapper.java
/** * Set the class that each row should be mapped to. * @param mappedClass the mapped class/* ww w. jav a 2 s. c om*/ */ protected synchronized void doSetMappedClass(Class mappedClass) { if (this.mappedClass == null) { initialize(mappedClass); } else { if (!this.mappedClass.equals(mappedClass)) { throw new InvalidDataAccessApiUsageException("The mapped class can not be reassigned to map to " + mappedClass + " since it is already providing mapping for " + this.mappedClass); } } }
From source file:org.springframework.jdbc.core.AbstractBeanPropertyRowMapper.java
protected Object doMapRow(ResultSet rs, int rowNumber) throws SQLException { if (getMappedClass() == null) throw new InvalidDataAccessApiUsageException("Target class was not specified - it is mandatory"); Object result;//from w ww . jav a 2 s . c o m try { result = this.defaultConstruct.newInstance((Object[]) null); } catch (IllegalAccessException e) { throw new DataAccessResourceFailureException("Failed to load class " + this.mappedClass.getName(), e); } catch (InvocationTargetException e) { throw new DataAccessResourceFailureException("Failed to load class " + this.mappedClass.getName(), e); } catch (InstantiationException e) { throw new DataAccessResourceFailureException("Failed to load class " + this.mappedClass.getName(), e); } ResultSetMetaData rsmd = rs.getMetaData(); int columns = rsmd.getColumnCount(); for (int i = 1; i <= columns; i++) { String column = JdbcUtils.lookupColumnName(rsmd, i).toLowerCase(); PersistentField fieldMeta = (PersistentField) this.mappedFields.get(column); if (fieldMeta != null) { BeanWrapper bw = new BeanWrapperImpl(mappedClass); bw.setWrappedInstance(result); fieldMeta.setSqlType(rsmd.getColumnType(i)); Object value = null; Class fieldType = fieldMeta.getJavaType(); if (fieldType.equals(String.class)) { value = rs.getString(column); } else if (fieldType.equals(byte.class) || fieldType.equals(Byte.class)) { value = new Byte(rs.getByte(column)); } else if (fieldType.equals(short.class) || fieldType.equals(Short.class)) { value = new Short(rs.getShort(column)); } else if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) { value = new Integer(rs.getInt(column)); } else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) { value = new Long(rs.getLong(column)); } else if (fieldType.equals(float.class) || fieldType.equals(Float.class)) { value = new Float(rs.getFloat(column)); } else if (fieldType.equals(double.class) || fieldType.equals(Double.class)) { value = new Double(rs.getDouble(column)); } else if (fieldType.equals(BigDecimal.class)) { value = rs.getBigDecimal(column); } else if (fieldType.equals(boolean.class) || fieldType.equals(Boolean.class)) { value = (rs.getBoolean(column)) ? Boolean.TRUE : Boolean.FALSE; } else if (fieldType.equals(java.util.Date.class) || fieldType.equals(java.sql.Timestamp.class) || fieldType.equals(java.sql.Time.class) || fieldType.equals(Number.class)) { value = JdbcUtils.getResultSetValue(rs, rs.findColumn(column)); } if (value != null) { if (bw.isWritableProperty(fieldMeta.getFieldName())) { try { if (logger.isDebugEnabled() && rowNumber == 0) { logger.debug("Mapping column named \"" + column + "\"" + " containing values of SQL type " + fieldMeta.getSqlType() + " to property \"" + fieldMeta.getFieldName() + "\"" + " of type " + fieldMeta.getJavaType()); } bw.setPropertyValue(fieldMeta.getFieldName(), value); } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column " + column + " to property " + fieldMeta.getFieldName(), ex); } } else { if (rowNumber == 0) { logger.warn("Unable to access the setter for " + fieldMeta.getFieldName() + ". Check that " + "set" + StringUtils.capitalize(fieldMeta.getFieldName()) + " is declared and has public access."); } } } } } return result; }
From source file:org.springframework.jdbc.core.BeanPropertyRowMapper.java
/** * Extract the values for all columns in the current row. * <p>Utilizes public setters and result set metadata. * @see java.sql.ResultSetMetaData/*w w w .j a va2 s. co m*/ */ @Override public T mapRow(ResultSet rs, int rowNumber) throws SQLException { Assert.state(this.mappedClass != null, "Mapped class was not specified"); T mappedObject = BeanUtils.instantiateClass(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<>() : null); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index); String field = lowerCaseName(column.replaceAll(" ", "")); PropertyDescriptor pd = (this.mappedFields != null ? this.mappedFields.get(field) : null); if (pd != null) { try { Object value = getColumnValue(rs, index, pd); if (rowNumber == 0 && logger.isDebugEnabled()) { logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type '" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "'"); } try { bw.setPropertyValue(pd.getName(), value); } catch (TypeMismatchException ex) { if (value == null && this.primitivesDefaultedForNullValue) { if (logger.isDebugEnabled()) { logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with null value when setting property '" + pd.getName() + "' of type '" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "' on object: " + mappedObject, ex); } } else { throw ex; } } if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column '" + column + "' to property '" + pd.getName() + "'", ex); } } else { // No PropertyDescriptor found if (rowNumber == 0 && logger.isDebugEnabled()) { logger.debug("No property found for column '" + column + "' mapped to field '" + field + "'"); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException( "Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass.getName() + "]: " + this.mappedProperties); } return mappedObject; }
From source file:org.springframework.jdbc.core.JdbcTemplate.java
public Object query(final String sql, final ResultSetExtractor rse) throws DataAccessException { if (sql == null) { throw new InvalidDataAccessApiUsageException("SQL must not be null"); }//from w w w. j a va2 s . c o m if (JdbcUtils.countParameterPlaceholders(sql, '?', "'\"") > 0) { throw new InvalidDataAccessApiUsageException( "Cannot execute [" + sql + "] as a static query: it contains bind variables"); } if (logger.isDebugEnabled()) { logger.debug("Executing SQL query [" + sql + "]"); } class QueryStatementCallback implements StatementCallback, SqlProvider { public Object doInStatement(Statement stmt) throws SQLException { ResultSet rs = null; try { if (getFetchSize() > 0) stmt.setFetchSize(getFetchSize()); rs = stmt.executeQuery(sql); ResultSet rsToUse = rs; if (nativeJdbcExtractor != null) { rsToUse = nativeJdbcExtractor.getNativeResultSet(rs); } return rse.extractData(rsToUse); } finally { JdbcUtils.closeResultSet(rs); } } public String getSql() { return sql; } } return execute(new QueryStatementCallback()); }
From source file:org.springframework.jdbc.core.JdbcTemplate.java
public Object query(final String sql, final PreparedStatementSetter pss, final ResultSetExtractor rse) throws DataAccessException { if (sql == null) { throw new InvalidDataAccessApiUsageException("SQL may not be null"); }/*from w w w . ja v a 2 s.c om*/ return query(new SimplePreparedStatementCreator(sql), pss, rse); }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
public void testStaticResultSetClosed() throws Exception { MockControl ctrlResultSet;/*from w w w.j a v a2s .c om*/ ResultSet mockResultSet; MockControl ctrlStatement; Statement mockStatement; MockControl ctrlResultSet2; ResultSet mockResultSet2; MockControl ctrlPreparedStatement; PreparedStatement mockPreparedStatement; ctrlResultSet = MockControl.createControl(ResultSet.class); mockResultSet = (ResultSet) ctrlResultSet.getMock(); mockResultSet.close(); ctrlResultSet.setVoidCallable(); ctrlStatement = MockControl.createControl(Statement.class); mockStatement = (Statement) ctrlStatement.getMock(); mockStatement.executeQuery("my query"); ctrlStatement.setReturnValue(mockResultSet); mockStatement.close(); ctrlStatement.setVoidCallable(); ctrlResultSet2 = MockControl.createControl(ResultSet.class); mockResultSet2 = (ResultSet) ctrlResultSet2.getMock(); mockResultSet2.close(); ctrlResultSet2.setVoidCallable(); ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class); mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock(); mockPreparedStatement.executeQuery(); ctrlPreparedStatement.setReturnValue(mockResultSet2); mockPreparedStatement.close(); ctrlPreparedStatement.setVoidCallable(); mockConnection.createStatement(); ctrlConnection.setReturnValue(mockStatement); mockConnection.prepareStatement("my query"); ctrlConnection.setReturnValue(mockPreparedStatement); ctrlResultSet.replay(); ctrlStatement.replay(); ctrlResultSet2.replay(); ctrlPreparedStatement.replay(); replay(); JdbcTemplate template = new JdbcTemplate(mockDataSource); try { template.query("my query", new ResultSetExtractor() { public Object extractData(ResultSet rs) { throw new InvalidDataAccessApiUsageException(""); } }); fail("Should have thrown InvalidDataAccessApiUsageException"); } catch (InvalidDataAccessApiUsageException idaauex) { // ok } try { template.query(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection con) throws SQLException { return con.prepareStatement("my query"); } public String getSql() { return null; } }, new ResultSetExtractor() { public Object extractData(ResultSet rs2) { throw new InvalidDataAccessApiUsageException(""); } }); fail("Should have thrown InvalidDataAccessApiUsageException"); } catch (InvalidDataAccessApiUsageException idaauex) { // ok } // verify confirms if test is successful by checking if close() called ctrlResultSet.verify(); ctrlStatement.verify(); ctrlResultSet2.verify(); ctrlPreparedStatement.verify(); }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
public void testExecuteClosed() throws Exception { MockControl ctrlResultSet;/*from www . j av a2s .c o m*/ ResultSet mockResultSet; MockControl ctrlCallable; CallableStatement mockCallable; ctrlResultSet = MockControl.createControl(ResultSet.class); mockResultSet = (ResultSet) ctrlResultSet.getMock(); mockResultSet.next(); ctrlResultSet.setReturnValue(true); mockResultSet.close(); ctrlResultSet.setVoidCallable(); ctrlCallable = MockControl.createControl(CallableStatement.class); mockCallable = (CallableStatement) ctrlCallable.getMock(); mockCallable.execute(); ctrlCallable.setReturnValue(true); mockCallable.getUpdateCount(); ctrlCallable.setReturnValue(-1); mockCallable.getResultSet(); ctrlCallable.setReturnValue(mockResultSet); mockCallable.close(); ctrlCallable.setVoidCallable(); mockConnection.prepareCall("my query"); ctrlConnection.setReturnValue(mockCallable); ctrlResultSet.replay(); ctrlCallable.replay(); replay(); List params = new ArrayList(); params.add(new SqlReturnResultSet("", new RowCallbackHandler() { public void processRow(ResultSet rs) { throw new InvalidDataAccessApiUsageException(""); } })); JdbcTemplate template = new JdbcTemplate(mockDataSource); try { template.call(new CallableStatementCreator() { public CallableStatement createCallableStatement(Connection conn) throws SQLException { return conn.prepareCall("my query"); } }, params); } catch (InvalidDataAccessApiUsageException idaauex) { // ok } // verify confirms if test is successful by checking if close() called ctrlResultSet.verify(); ctrlCallable.verify(); }