Spring: java.io.Notserializableexception: Mapsqlparametersource

Asked

Viewed 136 times

0

I’m trying a simple Spring Jdbctemplate based Insert mapping query parameters with Mapsqlparamatersource and I’m making an error according to 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 setters

Can anyone tell me how to solve this problem ?

  • 1

    The SQL statement is not correct. You can post the javabean of the Account type?

  • Posted João Manolo.

  • 1

    A doubt... You have created an "Insert" statement, but you are giving "Update"?

  • Yes, for "Inserts" with Spring-Jdbc, you use the "Update" statement".

  • 1

    Got it! So I think he’s missing the attribute (parameter) gamedate, has tested?

  • So, if I were passing my Account object in the update argument, I would agree with you. But since I am using Mapsqlparametersource to map parameters and values, Spring does not require me to have all attributes of my object filled, you understand?

  • 1

    Yes I agree. Well, I think we could do the following test: you tried to replace the parameters passed in the SQL statement with the "?" joker. I believe that the way Query is currently only working with Namedparameterjdbctemplate

  • Namedparameterjdbctemplate worked perfectly.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.