01: /**
02: * User: Clinton Begin Date: Jul 13, 2003 Time: 7:21:30 PM
03: */package com.ibatis.jpetstore.persistence.sqlmapdao;
04:
05: import com.ibatis.dao.client.DaoException;
06: import com.ibatis.dao.client.DaoManager;
07: import com.ibatis.jpetstore.domain.Sequence;
08: import com.ibatis.jpetstore.persistence.iface.SequenceDao;
09:
10: public class SequenceSqlMapDao extends BaseSqlMapDao implements
11: SequenceDao {
12:
13: public SequenceSqlMapDao(DaoManager daoManager) {
14: super (daoManager);
15: }
16:
17: /**
18: * This is a generic sequence ID generator that is based on a database table
19: * called 'SEQUENCE', which contains two columns (NAME, NEXTID). <p/>This
20: * approach should work with any database.
21: *
22: * @param name
23: * The name of the sequence.
24: * @return The Next ID @
25: */
26: public synchronized int getNextId(String name) {
27: Sequence sequence = new Sequence(name, -1);
28:
29: sequence = (Sequence) queryForObject("getSequence", sequence);
30: if (sequence == null) {
31: throw new DaoException(
32: "Error: A null sequence was returned from the database (could not get next "
33: + name + " sequence).");
34: }
35: Object parameterObject = new Sequence(name, sequence
36: .getNextId() + 1);
37: update("updateSequence", parameterObject);
38:
39: return sequence.getNextId();
40: }
41:
42: }
|