package org.springframework.samples.jpetstore.dao.ibatis;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class SqlMapSequenceDao extends SqlMapClientDaoSupport {

  /**
   * This is a generic sequence ID generator that is based on a database
   * table called 'SEQUENCE', which contains two columns (NAME, NEXTID).
   * This approach should work with any database.
   * @param name the name of the sequence
   * @return the next ID
   */
  public int getNextId(String name) throws DataAccessException {
    Sequence sequence = new Sequence(name, -1);
    sequence = (Sequence) getSqlMapClientTemplate().queryForObject("getSequence", sequence);
    if (sequence == null) {
      throw new DataRetrievalFailureException("Error: A null sequence was returned from the database (could not get next " +
      			name + " sequence).");
    }
    Object parameterObject = new Sequence(name, sequence.getNextId() + 1);
    getSqlMapClientTemplate().update("updateSequence", parameterObject, 1);
    return sequence.getNextId();
  }
}

@@ -861,104 +861,65 @@
 ion(
-%22Error: A null sequence was returned from the database (could not get next %22 +%0A      %09%09%09
+%0A%09%09%09%09%09%22Could not get next value of sequence '%22 + 
 name + %22
  seq
@@ -914,16 +914,18 @@
 name + %22
+':
  sequenc
@@ -925,18 +925,31 @@
 sequence
-).
+ does not exist
 %22);%0A    
@@ -1138,10 +1138,11 @@
 ();%0A  %7D%0A
+%0A
 %7D%0A

clearer exception message if sequence not found