Dirk,
You need to do a SELECT MAX(KeyName) in the ejbPostCreate(). Then you set
the relevant member of the CMP bean to that value, and you pull out the
primary key from the entity context and set it to the same value. I guess
they put in ejbPostCreate for precisely this kind of eventuality. Here's an
example:
public class SomeEntityBean
{
<snip>
/**Matching ejbPostCreate for the ejbCreate.*/
public void ejbPostCreate(CollaborationObject collabObject)
throws CreateException, RemoteException
{
int iKey = EJBHelpers.getNewAutonumberValue
(
m_DataSource,"CollabObjectLog","CollaborationObjectLogKey"
);
CollaborationObjectLogKey = iKey;
CollaborationObjectLogEntryPK pk =
(CollaborationObjectLogEntryPK)m_EntityContext.getPrimaryKey();
pk.CollaborationObjectLogKey = iKey;
}
}
public class EJBHelpers
{
public static int getNewAutonumberValue(DataSource ds,String
sTableName,String sFieldName)
throws RemoteException
{
try
{
int iReturn = 0;
String sSQL = "select max(" + sFieldName + ") from " + sTableName;
Connection con = ds.getConnection();
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(sSQL);
if (rs.next()) iReturn = rs.getInt(1);
rs.close();
statement.close();
con.close();
return iReturn;
}
catch (SQLException e) {throw new RemoteException(null,e);}
}
}
If anyone knows a smarter/cleaner way to do this, please *do* let me know!
Joe
-----Original Message-----
From: Dirk Haase [SMTP:[EMAIL PROTECTED]]
Sent: Friday, March 10, 2000 1:32 PM
To: [EMAIL PROTECTED]
Subject: Auto-Increment Primary Keys
Hi,
how do I create CMP Entity Beans with a primary key that is mapped to a
database column that is set on auto-increment? Or is this only possible
with BMP?
thanx for help
Dirk
--
Dirk Haase mailto: [EMAIL PROTECTED]
Schlund & Partner
----
To unsubscribe, send email to [EMAIL PROTECTED] and
include in the body of the message "unsubscribe jonas-users".
For general help, send email to [EMAIL PROTECTED] and
include in the body of the message "help".
----
To unsubscribe, send email to [EMAIL PROTECTED] and
include in the body of the message "unsubscribe jonas-users".
For general help, send email to [EMAIL PROTECTED] and
include in the body of the message "help".