1

As per Spring 3.2 Data access docs, SimpleJdbcInsert can be use to retrieve auto generated keys. But, I cannot override the final method setDataSource from JdbcDaoSupport in the code below:

public class LoginDAOImpl extends JdbcDaoSupport implements LoginDAO {

    // Cannot override the final method from JdbcDaoSupport
    public void setDataSource(DataSource dataSource) {
    }

JdbcDaoSupport class is not extended in the Spring 3.2 doc. So, I have 2 questions:

  1. How to use SimpleJdbcInsert to retrieve auto-generated keys while extending JdbcDaoSupport class?

  2. If I do not extend JdbcDaoSupport then what shall be the code changes in the configuration file and dao class. Please find below current configuration and dao code:

configuration file:

    <bean id="loginDao" class="com.vikas.dao.LoginDAO"
    p:dataSource-ref="dataSource" />

relevant doa code:

getJdbcTemplate().update(...);
Vikas Sharma
  • 1,235
  • 2
  • 27
  • 53

3 Answers3

1

You can use a variation of SimpleJDBCInsert which takes JdbcTemplate as the constructor argument.

SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(getJdbcTemplate());

This way there is no need to try and get hold of the datasource directly.

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
0

After removing JdbcDaoSupport, I am able to retrieve auto-generated keys using SimpleJdbcInsert by following changes:

configuration file:

<bean id="loginDao" class="com.vikas.dao.LoginDAOImpl"
        p:dataSource-ref="dataSource" />

DAO class:

public class LoginDAOImpl implements LoginDAO {

    private JdbcTemplate jdbcTemplate;
    private SimpleJdbcInsert insertPerson;

    public void setDataSource(DataSource dataSource) {

        jdbcTemplate = new JdbcTemplate(dataSource);
        insertPerson = new SimpleJdbcInsert(dataSource).withTableName("PERSON")
                .usingGeneratedKeyColumns("PERSON_ID");
    }

    @Override
    public void addPerson(Person person) {

        SqlParameterSource parameters = new BeanPropertySqlParameterSource(
                person);
        Number newId = insertPerson.executeAndReturnKey(parameters);

        String sql = "INSERT INTO PERSON_ROLE (PERSON_ID, ROLE) VALUES (?, ?)";

        jdbcTemplate.update(sql, newId.longValue(), person.getPersonRole().getRole());
    }

}

I am still looking for a way to use SimpleJdbcInsert while extending JdbcDaoSupport.

Vikas Sharma
  • 1,235
  • 2
  • 27
  • 53
-1

auto generated key's are generated on insert, not on update

Pankaj Sharma
  • 1,833
  • 1
  • 17
  • 22
  • I am passing insert query to getJdbcTemplate().update("INSERT INTO PERSON ...", ...); Also there is no getJdbcTemplate().insert method. However, my question is regarding changes required to use SimpleJdbcInsert in existing JdbcDaoSupport implementation to retrieve auto generated keys. – Vikas Sharma Oct 12 '13 at 08:33
  • i am sorry bro. i find a link may be it can help you see the accepted answer .http://stackoverflow.com/questions/1665846/identity-from-sql-insert-via-jdbctemplate – Pankaj Sharma Oct 12 '13 at 08:44