Announcement Announcement Module
Collapse
No announcement yet.
java.io.NotSerializableException for BeanPropertySqlParameterSource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • java.io.NotSerializableException for BeanPropertySqlParameterSource

    Hello Guys

    I am working with:

    Spring Core 3.1.2.RELEASE, STS 3.0.0 + JDK 1.7

    I have the follow Entity

    Code:
    @SuppressWarnings("serial")
    public class Cliente implements Serializable{
    	
    	private String idCliente;
    	private String nombreCliente;
    	private String apellidoCliente;
    	private Date fechaCliente;
    
    //setters/getters
    
    //toString
    }
    the table is

    Code:
    create table cliente(
    
    	idCliente varchar(10) not null,
    	nombreCliente varchar(20) not null,
    	apellidoCliente varchar(20) not null,
    	fechaCliente timestamp not null,
      
    	PRIMARY KEY(idCliente)
    	 
    )ENGINE=InnoDB;
    Therefore same fields/variables in a 100%

    My Dao Method

    Code:
    @Transactional
    @Repository
    public class ClienteJdbcDaoImpl implements ClienteDaoService{
    	
    	@Autowired
    	private JdbcTemplate jdbcTemplate;
    	
    	@Override
    	public void insertarCliente(Cliente cliente) {
    		
    		SqlParameterSource sqlParameterSource = 
    				new BeanPropertySqlParameterSource(cliente);
    		
    		jdbcTemplate.update("INSERT INTO cliente(idCliente, nombreCliente, apellidoCliente, fechaCliente) " +
    	            		    "VALUES(:idCliente, :nombreCliente, :apellidoCliente, :fechaCliente) ",
    	            		    sqlParameterSource		            
    	            );		
    	}
    }
    My Main class

    Code:
    ClienteBoService clienteBoService =
    				(ClienteBoService) context.getBean(ClienteBoService.class);
    		
    Cliente cliente = new Cliente();
    	
    cliente.setIdCliente("MJE");
    cliente.setNombreCliente("Manuel");
    cliente.setApellidoCliente("Jordan");
    cliente.setFechaCliente(new Date());
    	
    clienteBoService.insertarCliente(cliente);
    But when I execute the code I always get

    Code:
    Caused by: java.sql.SQLException: Invalid argument value: java.io.NotSerializableException
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
            ...
    	... 28 more
    Caused by: java.io.NotSerializableException: org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource
    	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
    	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    	at com.mysql.jdbc.PreparedStatement.setSerializableObject(PreparedStatement.java:4393)
    	... 38 more
    What is wrong?

    I did the same than the code used in Spring Recipes 2nd Edition, the unique difference is that in the book it is working with the SimpleJdbcTemplate class where it is now deprecated and the API suggest use the jdbcTemplate.
    I am doing this

    Even more, even If use

    Code:
    Map<String, Object> parameters = new HashMap<String,Object>();
    		
    parameters.put("idCliente", cliente.getIdCliente());
    parameters.put("nombreCliente",cliente.getNombreCliente());
    parameters.put("apellidoCliente",cliente.getApellidoCliente());
    parameters.put("fechaCliente",cliente.getFechaCliente());
    		
    SqlParameterSource sqlParameterSource = 
    		new MapSqlParameterSource(parameters);
    I got the same results

    What is wrong?

    Thanks in advanced

  • #2
    I suggest a read of the docs ...

    JdbcTemplate doesn't have a method which uses a SqlParameterSource only the NamedParameterJdbcTemplate has one...

    What is wrong?
    Using the wrong template...

    Comment


    • #3
      Hello Marten

      Have sense your idea, I will check it out tonight or tomorrow

      Thank You.

      Comment

      Working...
      X