Get row id after EJB create method.
Hi,
does any one know how to get an id of new created by CMP ejb row? When I create new object it's id is 0, when i call ejb create it creates new row in DB and crates new ID by increasing the old one, but how can I get this new ID after create has finished?
Now I have to look equals by some fields object in DB after creation, looks ugly.
[360 byte] By [
JAntona] at [2007-11-27 11:25:00]

# 2
So here is my CMP :
/**
* @ejb.bean name="Dividend"
* type="CMP"
* view-type="local"
* primkey-field="id"
* schema="dividends"
* cmp-version="2.x"
* @ejb.value-object name="Dividend"
* match="*"
* @ejb.transaction type="Required"
* @ejb.persistence table-name="dividends"
* @ejb.finder signature="Collection findAll()"
* query="SELECT OBJECT(d) FROM dividends d"
* @jboss.query signature="Collection findAll()"
* @ejb.finder signature="Dividend findByActivityId(java.lang.Integer activityId)"
* query="SELECT OBJECT(d) FROM dividends d WHERE d.activityId = ?1"
* @jboss.query signature="Dividend findByActivityId(java.lang.Integer activityId)"
* strategy="on-load"
* @jboss.persistence create-table="false"
* remove-table="false"
* datasource="java:/ChronimDS"
* datasource-mapping="mySQL"
*/
public abstract class DividendBean implements EntityBean {
//~ Methods -
//==========================================
// Business methods
//==========================================
/**
* @ejb.interface-method
*/
public Dividend getDividend() {
Dividend dividend = new Dividend();
dividend.setId(this.getId());
dividend.setActivityId(this.getActivityId());
dividend.setLower(this.getIsLower());
dividend.setLowerAmount(this.getLowerAmount());
return dividend;
}
/**
* @ejb.interface-method
*/
public void setDividend(Dividend dividend) {
this.setActivityId(dividend.getActivityId());
this.setIsLower(dividend.isLower());
this.setLowerAmount(dividend.getLowerAmount());
}
//==========================================
// CMP fields
//==========================================
/**
* @ejb.pk-field
* @ejb.persistence column-name="id"
* jdbc-type="INTEGER"
* sql-type="INTEGER"
* @ejb.interface-method
* @ejb.transaction type="NotSupported"
*/
public abstract Integer getId();
public abstract void setId(Integer id);
/**
* @ejb.persistence column-name="activityId"
* jdbc-type="INTEGER"
* sql-type="INTEGER"
*/
public abstract Integer getActivityId();
public abstract void setActivityId(Integer activityId);
/**
* @ejb.persistence column-name="isLower"
* jdbc-type="TINYINT"
* sql-type="TINYINT"
*/
public abstract boolean getIsLower();
public abstract void setIsLower(boolean isLower);
/**
* @ejb.persistence column-name="lowerAmount"
* jdbc-type="INTEGER"
* sql-type="INTEGER"
*/
public abstract Integer getLowerAmount();
public abstract void setLowerAmount(Integer lowerAmount);
//==========================================
// EJB callbacks
//==========================================
/**
* @ejb.create-method
*/
public Integer ejbCreate(Dividend dividend)
throws CreateException {
this.setId(dividend.getId());
this.setIsLower(dividend.isLower());
this.setLowerAmount(dividend.getLowerAmount());
this.setActivityId(dividend.getActivityId());
return null;
}
public void ejbPostCreate(Dividend dividend)
throws CreateException {
}
// EntityBean (empty) implementation
//
public void setEntityContext(javax.ejb.EntityContext ec) {
}
public void unsetEntityContext() {
}
public void ejbLoad() {
}
public void ejbStore() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void ejbRemove() {
}
}
As you can see I'm using xdoclets.
Suppose I need to create new row of this object :
Dividend d = new Dividend();
...
// fill dividend by values here but do not set it's id it is 0 before creation
// SQL server will set it automaticly
...
Object ref = context.lookup("DividendLocal");
DividendLocalHome dividendLocal = (DividendLocalHome) ref;
dividendLocal.create(dividend)
I create new row of dividend in my DB and want to get an ID of the new created row, I thought that dividend object will get this ID after creation automaticly, but no, his id is 0.
so here id will be 0 :
dividendLocal.create(dividend);
int id = dividend.getId(); // 0
and if I write this
dividendLocal.create(dividend).getId();
I get :
2007-06-25 14:44:02,384 ERROR [org.jboss.ejb.plugins.LogInterceptor]
EJBException in method: public abstract java.lang.Integer
com.xxx.xxx.ejb.SessionFacade.createDividend(
xxx.xxx.model.Contract) throws java.rmi.RemoteException:
javax.ejb.NoSuchObjectLocalException: Entity not found: primaryKey=0