OSCore provides a couple helper classes that can be used when building EJB session beans or entity beans. The classes are in the com.opensymphony.ejb.* package. The first class, SessionAdapter, implements javax.ejb.SessionBean and provides default implementation of the ejbXxxx() methods such as ejbSetSessionContext. While not totally, neccessary, you'll find yourself duplicating a lot of code if you don't take advantage of this simple class.
The second and more useful class is the EntityAdapter class. This class implements javax.ejb.EntityBean and provides the same features that the SessionAdapter does, but also extra entity-specific features such as sequence generation and automated PropertySet access. The sequence generator is especially useful in the ejbCreate() method. Here you can get a new primary key using the methods nextLong() or nextInt(). This method automatically connects to the SequenceGenerator module and asks for next ID in the sequence for this type of entity bean. It is important that you provide an ejb-ref to the SequenceGenerator EJB and also an env-entry of the name sequenceName to provide the SequenceGenerator EJB the name of the sequence you wish to use. The following is an example snipplet of an ejb-jar.xml file that is used for an EJB extending EntityAdapter:
... <env-entry>
<env-entry-name>sequenceName</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>fooSequence</env-entry-value>
</env-entry>
<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> ...
The EntityAdapter class also provides an automatic interface in to the EJBPropertySet module. This is accomplished by calling the locatePropertySet(long id) method inside your entity bean. This method returns a PropertySet object that is backed by Enterprise JavaBeans. In order to use this feature, you must specify the sequenceName env-entry above, as it is required for the EJBPropertySet to locate to correct values. EntityAdapter will attempt to look up the PropertyStore EJB without any ejb-ref specified in your ejb-jar.xml file, but we recommend you add the following to your deployment descriptor to optimize PropertySet access in your entity beans:
<ejb-ref> <ejb-ref-name>ejb/os.PropertyStore</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home>com.opensymphony.module.propertyset.ejb.PropertyStoreHome</home> <remote>com.opensymphony.module.propertyset.ejb.PropertyStore</remote> <ejb-link>os.PropertyStore</ejb-link> </ejb-ref>