Discussion:
[Middlegen-user] using xdoclet to have cmp auto-generate primary key values
m***@hyperreal.org
2004-04-02 19:07:02 UTC
Permalink
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() { }
}
Darren Hartford
2004-04-02 19:26:09 UTC
Permalink
Hey Mike,
A quick and poor answer for you. Xdoclet/ejbdoclet itself does not offer support for key generation, so you will need to look to an external source for the generation class(es).

If using the Jboss App server, recommend looking at the Jboss docs on key generation. They have quite the section on how to do this, as well as detailing pros/cons to different methods (I remember they have a newer DB-based-keygen that is pretty fast...).

I think Middlegen does have some of its own built-in key generation classes, but I've never used Middlegen's classes and not sure if there are any examples on how to use theirs.

Another solution, although kind of overkill, is Hibernate also contains some keygen classes you could use.

Hope this helps!
-D

Loading...