Skip to content

JDBCTemplate queryForObject returning an empty list with null values [SPR-15392] #19955

@spring-projects-issues

Description

@spring-projects-issues

sneha ghosh opened SPR-15392 and commented

JDBCTemplate.java
We are calling this method from our application
@Override
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType)
throws DataAccessException {

	return queryForObject(sql, args, argTypes, getSingleColumnRowMapper(requiredType));
}

the internal implementation would call:
@Override
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
throws DataAccessException {

	List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<T>(rowMapper, 1));
	return DataAccessUtils.requiredSingleResult(results);
}

Result is being returned as an arrayList of size 1 with null values in it incase the database returns no records. this causes DataAccessUtils.requiredSingleResult(results) to throw a null pointer exception when iterating over a null list. is there a solution for this ?

public static <T> T requiredSingleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException {
	int size = (results != null ? results.size() : 0);
	if (size == 0) {
		throw new EmptyResultDataAccessException(1);
	}
	if (results.size() > 1) {
		throw new IncorrectResultSizeDataAccessException(1, size);
	}
	return results.iterator().next();
}

Attachments:

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions