This may seem like an odd request, but here goes...

I'm using JBoss 3.2.3 with PostgreSQL 7.3.2, and I have a CMP entity bean
(coded using ejbdoclet) that has an integer primary key. The ejbCreate code
currently generates the value for the primary key by doing a "select max(id)
from table", which is really slow because it has to access the database
twice (the select and the insert).

I understand that it's possible to have the entity bean use the database to
auto-generate new primary key values. (With postgres, it has been
recommended to use a database sequence to generate the values.)

Supposedly it's possible to do this in ejbdoclet, but I can't find any
reasonable documentation or an example to copy.  I then discovered that
Middlegen can generate the ejbdoclet file for me.  I downloaded Middlegen
and have been unsuccessful in getting it working in my environment.

But my ultimate goal isn't to get Middlegen running.  All I want is an
example ejbdoclet file for a CMP entity bean that has its primary key value
auto-generated by a PostgreSQL sequence.

Following is the source code for the entity bean.  Could someone show me how
to change it so that the primary key value is auto-generated by a PostgreSQL
sequence?  Or, could someone email me an xdoclet file of some other CMP
entity bean that uses a PostgreSQL sequence to generate the primary key
value?


Thanks,
Mike


--------- ejbdoclet file follows ---------



import javax.ejb.*;
import java.sql.*;


/**
 * @ejb.bean name="Item"
 * jndi-name="Item"
 * local-jndi-name="ItemLocal"
 * type="CMP" cmp-version="2.x"
 * view-type="both"
 *
 * @ejb.home remote-class="com.blah.ejb.entity.item.ItemHome"
 *           local-class="com.blah.ejb.entity.item.ItemLocalHome"
 *
 *
 * @ejb.interface remote-class="com.blah.ejb.entity.item.Item"
 *                local-class="com.blah.ejb.entity.item.ItemLocal"
 *
 * @ejb.persistence table-name="Item"
 *
 * @jboss.persistence create-table="false" remove-table="false"
 *
 * @ejb.transaction type="Required"
 *
 * @ejb.pk class="com.blah.ejb.entity.item.ItemPK"
 *
 */

public abstract class ItemEJB implements EntityBean {

        protected EntityContext entityContext = null;


        ///////////////////////////////////////////////////////
        // Container Managed Persistent Fields
        ///////////////////////////////////////////////////////

        /**
         * @ejb.interface-method view-type="both"
         * @ejb.persistence column-name="itemID"
         * @ejb:pk-field
         */
        public abstract int getItemID();

        /**
         * @ejb.interface-method view-type="both"
         * @ejb.persistence column-name="itemID"
         */
        public abstract void setItemID(int itemID);


        /**
         * @ejb.interface-method view-type="both"
         * @ejb.persistence column-name="name"
         */
        public abstract String getName();

        /**
         * @ejb.interface-method view-type="both"
         * @ejb.persistence column-name="name"
         */
        public abstract void setName(String name);


        ///////////////////////////////////////////////////////
        // Create methods
        ///////////////////////////////////////////////////////

        /**
         * @ejb.create-method view-type="both"
         */
        public ItemPK ejbCreate(String name) throws CreateException {
                synchronized(Item.class) {
                        int itemID = 0;

                        Connection con = null;
                        PreparedStatement ps = null;
                        try {
                                con = JNDICache.getConnection();
                                ps = con.prepareStatement("select MAX(ItemID) from 
ITEM");
                                ResultSet rs = ps.executeQuery();
                                rs.next();
                                itemID = rs.getInt(1) + 1;
                                rs.close();
                        }
                        catch (SQLException se) {
                        }
                        finally {
                                ps.close(); on.close();
                        }

                        setItemID(itemID);
                        setName(name);

                        return new ItemPK(itemID);
                }
        }

        public void ejbPostCreate(String name) {
        }


        ///////////////////////////////////////////////////////
        // Other required methods
        ///////////////////////////////////////////////////////

        public void setEntityContext(EntityContext ec) {
                entityContext = ec;
        }

        public void unsetEntityContext() {
                entityContext = null;
        }

        public void ejbActivate() { }
        public void ejbPassivate() { }
        public void ejbLoad() { }
        public void ejbStore() { }
        public void ejbRemove() { }
}

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.634 / Virus Database: 406 - Release Date: 3/18/2004



-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
middlegen-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/middlegen-user

Reply via email to