OSCore SequenceGenerator

The SequenceGenerator module is an EJB-only module and provides a standard way to generate ID values for CMP entity beans, a feature left out from the EJB specification. If your entity bean extends com.opensymphony.ejb.EntityAdapter, a new ID can be automatically retrieved via the nextLong() or nextId() methods. More info about the EntityAdapter can be found here.

SequenceGenerator is an EJB module that provides a very efficient unique ID generator. The primary use of this is as auto-incrementing primary-keys in entity beans, using a high/low algorithm. The following example code shows how one can a new ID in the sequence named "testSequence":

	SequenceGeneratorHome sgh = (SequenceGeneratorHome)EJBUtils.lookup("ejb/os.SequenceGenerator");
	SequenceGenerator sg = sgh.create();
	long nextId = sg.getCount("testSequence");

If you are using an entity bean that extends EntityAdapter, you can get a new ID even easier:

	public class FooBean extends EntityAdapter {
	...
		public Long ejbCreate() {
			Long id = new Long(nextLong());
			setId(id);
			return id;		
		}
	...
	}

We recommend that if you are going to access the SeqenceGenerator from an EJB or a JSP/Servlet, you place the following ejb-ref in your deployment descriptor. You'll also want to map this ejb-ref to the jndi-name os.SequenceGenerator using a vendor-specific deployment descriptor:

	<ejb-ref>
		<ejb-ref-name>ejb/os.SequenceGenerator</ejb-ref-name>
		<ejb-ref-type>Session</ejb-ref-type>
		<home>com.opensymphony.module.sequence.SequenceGeneratorHome</home>
		<remote>com.opensymphony.module.sequence.SequenceGenerator</remote>
		<ejb-link>os.SequenceGenerator</ejb-link>
	</ejb-ref>

Lastly, the default CMP mappings that the Sequence CMP EJB uses require the following table be created. Please be aware this SQL call has only been tested with Oracle and Hypersonic databases. You may need to alter the following call and/or CMP mappings to get OSCore to deploy on your database:

	create table OS_SEQUENCE (name VARCHAR2(255) not null primary key, count NUMBER    null)