KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > jpetstore > dao > ibatis > SqlMapSequenceDao


1 package org.springframework.samples.jpetstore.dao.ibatis;
2
3 import org.springframework.dao.DataAccessException;
4 import org.springframework.dao.DataRetrievalFailureException;
5 import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
6
7 public class SqlMapSequenceDao extends SqlMapClientDaoSupport {
8
9   /**
10    * This is a generic sequence ID generator that is based on a database
11    * table called 'SEQUENCE', which contains two columns (NAME, NEXTID).
12    * This approach should work with any database.
13    * @param name the name of the sequence
14    * @return the next ID
15    */

16   public int getNextId(String JavaDoc name) throws DataAccessException {
17     Sequence sequence = new Sequence(name, -1);
18     sequence = (Sequence) getSqlMapClientTemplate().queryForObject("getSequence", sequence);
19     if (sequence == null) {
20       throw new DataRetrievalFailureException("Error: A null sequence was returned from the database (could not get next " +
21                 name + " sequence).");
22     }
23     Object JavaDoc parameterObject = new Sequence(name, sequence.getNextId() + 1);
24     getSqlMapClientTemplate().update("updateSequence", parameterObject, 1);
25     return sequence.getNextId();
26   }
27 }
28
Popular Tags