Announcement Announcement Module
No announcement yet.
Using MapSqlParameterSource with a custom class? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using MapSqlParameterSource with a custom class?


    I want to use the JDBC Support by using the NamedParameterJDBCTemplate in combination with the MapSqlParameterSource.

    Lets say I have a DB-Table with userdata. I want to get one user like that :

    SqlParameterSource param = new MapSqlParameterSource("userid",1);

    That works fine and I get the data of user 1. But now I want to make a custom UserId Class. Lets say like

    public class UserId {
    protected Long id;

    public Long getId() {
    return id;

    public void setId(long id) { = new Long(id);

    At the moment I have to use it like

    SqlParameterSource param = new MapSqlParameterSource("userid",userId.getId());

    to work. But how can I use it like

    SqlParameterSource param = new MapSqlParameterSource("userid",userId);

    I tried by overwriting the toString() Method and/or by adding "Implements Serializable" to the classdefintion - but I always get an exception and it does not work.

    Any one can help me?

    Thanks alot!

    Regards, Heiko

  • #2
    Have you tried something like this?
    String SQL = "select * from User_Table where userid = :userid";
    return getSimpleJdbcTemplate().query(
    , new UserMapper()
    , new MapSqlParameterSource()
    .addValue("userid",userId.getId(), Types.INTEGER)

    Which version of Spring? Which SDK? What is the exception?