1

I'm trying to insert a simple base with Spring JdbcTemplate mapping the query of parameters with MapSqlParamaterSource and am taking error as the data below:

public void adiciona(Conta conta) {
  String sql = "insert into contas (descricao, paga, valor, tipo) values (:descricao,:paga,:valor,:tipo)";
  MapSqlParameterSource pss = new MapSqlParameterSource();
  pss.addValue("descricao", conta.getDescricao());
  pss.addValue("paga", "true");
  pss.addValue("valor", conta.getValor());
  pss.addValue("tipo", conta.getTipo());
  getJdbcTemplate().update(sql, pss);     
}

Error log:

mar 22, 2015 12:16:00 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [spring mvc] in context with path [/contas] threw exception [Request processing failed; nested exception is org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [insert into contas (descricao, paga, valor, tipo) values (:descricao,:paga,:valor,:tipo)]; Invalid argument value: java.io.NotSerializableException; nested exception is java.sql.SQLException: Invalid argument value: java.io.NotSerializableException] with root cause
java.io.NotSerializableException: org.springframework.jdbc.core.namedparam.MapSqlParameterSource

Java Bean:

public class Conta implements Serializable {

  private static final long serialVersionUID = 4678852901357132238L;

  private Long id;
  private String descricao;
  private boolean paga;
  private double valor;
  private Calendar dataPagamento;
  private TipoDaConta tipo;

  // getters and settes

Someone can tell me how to solve this problem?

0

2 Answers 2

2

I also faced the same issue, I resolved it by changing it from

this.getJdbcTemplate().update(query,mapSqlParameterSource);

to

this.getNamedParameterJdbcTemplate().update(query,mapSqlParameterSource);
Sign up to request clarification or add additional context in comments.

Comments

1

Try something like:

 this.getJdbcTemplate().update(updateStatement, new Object[] {conta.getDescricao(), "true", conta.getValor(), conta.getTipo()});

3 Comments

Thanks, but I want to use MapSqlParameterSource in my queries, can you help me?
Yes, I understand. There is another way to use the MapSqlParameter in queries for the Spring-Jdbc?
If you are really interested in using MapSqlParameter then change your jdbc template to named parameter jdbc template defined like <bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" id="namedParameterJdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.