The paragraph is copied from A. Ramu Meda's Bullet Notes:
"With bean-managed persistence, when the
EJB container moves an instance from the pooled state to the ready state, it does not automatically set the primary key. Therefore, the ejbCreate and ejbActivate methods must set the primary key. "
I don't quite understand what it is saying here. I checked the sample code for BMP bean from Ed Roman's bok MEJB 2rd Ed. The ejbCreate returns a primary key, but ejbActivate doesn't nothing related to the primary key.
public AccountPK ejbCreate(
String accountID, String ownerName)
throws CreateException {
PreparedStatement pstmt = null;
Connection conn = null;
try {
System.out.println("ejbCreate() called.");
this.accountID = accountID;
this.ownerName = ownerName;
this.balance = 0;
/*
* Acquire DB connection
*/
conn = getConnection();
/*
* Insert the account into the database
*/
pstmt = conn.prepareStatement(
"insert into accounts (id, ownerName, balance)"
+ " values (?, ?, ?)");
pstmt.setString(1, accountID);
pstmt.setString(2, ownerName);
pstmt.setDouble(3, balance);
pstmt.executeUpdate();
/*
* Generate the Primary Key and return it
*/
return new AccountPK(accountID);
}
catch (Exception e) {
throw new CreateException(e.toString());
}
finally {
/*
* Release DB Connection for other beans
*/
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}
}
public void ejbActivate() {
System.out.println("ejbActivate() called.");
}
Can somebody explain what's the quoted paragraph is saying in more detail? Thanks!
-David.