KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > jpetstore > persistence > sqlmapdao > SequenceSqlMapDao


1 /**
2  * User: Clinton Begin
3  * Date: Jul 13, 2003
4  * Time: 7:21:30 PM
5  */

6 package com.ibatis.jpetstore.persistence.sqlmapdao;
7
8 import com.ibatis.dao.client.DaoException;
9 import com.ibatis.dao.client.DaoManager;
10 import com.ibatis.jpetstore.domain.Sequence;
11 import com.ibatis.jpetstore.persistence.iface.SequenceDao;
12
13 public class SequenceSqlMapDao extends BaseSqlMapDao implements SequenceDao {
14
15   public SequenceSqlMapDao(DaoManager daoManager) {
16     super(daoManager);
17   }
18
19   /**
20    * This is a generic sequence ID generator that is based on a database
21    * table called 'SEQUENCE', which contains two columns (NAME, NEXTID).
22    * <p/>
23    * This approach should work with any database.
24    *
25    * @param name The name of the sequence.
26    * @return The Next ID
27    * @
28    */

29   public synchronized int getNextId(String JavaDoc name) {
30     Sequence sequence = new Sequence(name, -1);
31
32     sequence = (Sequence) queryForObject("getSequence", sequence);
33     if (sequence == null) {
34       throw new DaoException("Error: A null sequence was returned from the database (could not get next " + name + " sequence).");
35     }
36     Object JavaDoc parameterObject = new Sequence(name, sequence.getNextId() + 1);
37     update("updateSequence", parameterObject);
38
39     return sequence.getNextId();
40   }
41
42 }
43
Popular Tags