PDA

View Full Version : Possible Bug With JdbcTemplate getObject



sethladd
Sep 14th, 2004, 03:53 PM
Hello,

The javadocs say that getObject(String, Object[], Class) will return NULL if the result of the query is NULL.

I am testing for a NULL in my unit tests, and instead of getting NULL from getObject, I get an exception:



org.springframework.dao.IncorrectResultSizeDataAcc essException: Expected single row but found none
at org.springframework.jdbc.core.JdbcTemplate$ObjectR esultSetExtractor.extractData(JdbcTemplate.java:98 2)
at org.springframework.jdbc.core.JdbcTemplate$1.doInP reparedStatement(JdbcTemplate.java:390)
at org.springframework.jdbc.core.JdbcTemplate.execute (JdbcTemplate.java:334)
at org.springframework.jdbc.core.JdbcTemplate.query(J dbcTemplate.java:375)
at org.springframework.jdbc.core.JdbcTemplate.query(J dbcTemplate.java:411)
at org.springframework.jdbc.core.JdbcTemplate.queryFo rObject(JdbcTemplate.java:470)
at com.hic.eboss.dao.BrimsDAOJdbcImpl.getBusinessName (BrimsDAOJdbcImpl.java:99)
at


I think this isn't correct, given the javadocs. I am using a CVS build right before 1.1 release (a few days before 1.1)

Am I misinterpreting the javadocs?

Thanks!
Seth

Maarten
Sep 14th, 2004, 04:32 PM
The javadocs say that getObject(String, Object[], Class) will return NULL if the result of the query is NULL

You probably mean queryForObject ?

There is a difference between a query returning NULL and a query returning zero rows.

zero rows: select salary from emp where 1 = 2
null result: select max(salary) from emp where 1 = 2

I guess JdbcTemplate.queryForObject() will throw an exception for the first query and return null for the second query.

Maarten

sethladd
Sep 14th, 2004, 05:31 PM
You probably mean queryForObject ?


Doh! You're right, I'm talking about queryForObject.



There is a difference between a query returning NULL and a query returning zero rows.


Ahh... I see. You're right. Thanks for clearing that up for me!

Seth

acnesiac
Mar 16th, 2007, 06:01 PM
public UsuarioVO findUsuarioByLogin(String login) {
UsuarioVO usuarioVO = null;
try{
usuarioVO = (UsuarioVO)getJdbcTemplate().queryForObject("select * from where 1 = 2 ",new Object[] {login},new RowMapper());
}
catch (IncorrectResultSizeDataAccessException e) {
usuarioVO = null;
} return usuarioVO;
}


should i have to deal with the Exception like this or is there any other better and simple way to retrieve an object ?

thanks in advance !

karldmoore
Mar 17th, 2007, 05:29 AM
There are a couple of options; you either find all results and manage the problems of what is returned yourself (e.g. no results, more that one, etc....) or you queryForObject() and manage the exceptions that are thrown. I'd prefer the latter as it handles most of the effort for you.