Since this is a common problem that ends up getting asked a lot, and there
are few published good ways to go about it... it turns out orion has a
file in the news app called "counter.jar" that does just this. Here's the
quick-and-dirty on its use:

1) Add the counter.jar to the application.xml file as an ejb module.

2) Make sure that the ejb-jar.xml contains a reference to the Counter
bean, which should look something like this:

                <ejb-ref>
                        <description>The id counter for the
entity</description>
                        <ejb-ref-name>ejb/Counter</ejb-ref-name>
                        <ejb-ref-type>Entity</ejb-ref-type>
                        <home>com.evermind.ejb.CounterHome</home>
                        <remote>com.evermind.ejb.Counter</remote>
                </ejb-ref>

3) In your EJB code, have this kind of sequence:

public void ejbCreate() throws RemoteException, CreateException 
        {
        try
                {
                        CounterHome chome=(CounterHome)new
InitialContext().lookup("java:comp/env/ejb/Counter");
                        Counter ctr;
                        try
                                {
                                ctr=chome.findByPrimaryKey("mybeannameid");
                                }
                        catch(FinderException fe)
                                {
                                ctr=chome.create("mybeannameid");
                                }
                        id=ctr.getNextID());
                }
        catch(NamingException ne)
                {
                throw new CreateException("NamingException:\nCould not"+
" create EJB: "+ne.getMessage());
                }
        }

This will set id - a long - to the next available sequence.

-----------------------------------------------------------
Joseph B. Ottinger               [EMAIL PROTECTED]
http://cupid.suninternet.com/~joeo      HOMES.COM Developer


Reply via email to