Spring JDBC queryForObject with BigDecimal error

Java 1.7 / Spring 3.1

See below code.

BigDecimal value = queryAsObject (BigDecimal.class, "select balance from financial.accounts where account_id = ?", accountId); 

Where queryAsObject comes from an abstract parent class that basically performs CRUD operations.

 public <T> T queryAsObject(Class<T> modelClass, String sql, Object... args) { return jdbcTemplate.queryForObject(sql, new HawkBeanPropertyRowMapper<T>(modelClass), args); } 

Pretty direct call to spring jdbc. However, this leads to the following exception:

 org.springframework.web.util.NestedServletException: Request processing failed; nested exception in org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.math.BigDecimal]: Is it an abstract class?; nested exception is java.lang.InstantiationException: java.math.BigDecimal 

With root cause:

 org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.math.BigDecimal]: Is it an abstract class?; nested exception is java.lang.InstantiationException: java.math.BigDecimal org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:81) org.springframework.jdbc.core.BeanPropertyRowMapper.mapRow(BeanPropertyRowMapper.java:233) org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:92) org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:1) org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:649) org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587) org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637) org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:666) org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:674) org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:734) 

What does it mean?

+4
source share
3 answers

I think JdbcTemplate.queryForObject(String sql, RowMapper<T> rowMapper, Object... args) is for binding ResultSet to Beans / POJO.

You need another overloaded version of queryForObject(String sql, Class<T> requiredType, Object... args) , i.e.

 public <T> T queryAsScalar(Class<T> scalarClass, String sql, Object... args) { return jdbcTemplate.queryForObject(sql, scalarClass, args); } 
+3
source

Your code uses a subclass of BeanPropertyMapper . It considers the class provided as a Java bean, creating it by calling the default constructor and then setting properties by calling setters. java.math.BigDecimal does not have a default constructor and that it does not work.

Instead, you should use the queryForObject(String sql, Class<T> requiredType, Object... args) method queryForObject(String sql, Class<T> requiredType, Object... args) .

+2
source

It definitely works

 JdbcTemplate t = new JdbcTemplate(ds); BigDecimal id = t.queryForObject("select id from bank where id=1", BigDecimal.class); 
+1
source

All Articles