wood burning stoves 2.0*
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes set the primary key in the ejbCreate and ejbActivate methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "set the primary key in the ejbCreate and ejbActivate methods?" Watch "set the primary key in the ejbCreate and ejbActivate methods?" New topic
Author

set the primary key in the ejbCreate and ejbActivate methods?

David Yuan
Greenhorn

Joined: Mar 01, 2004
Posts: 15
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.
Willie Vu
Greenhorn

Joined: Mar 16, 2004
Posts: 13
Originally posted by David Yuan:
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. "
...

It is just wrong. According to the EJB 2 spec, Section 12.1.4.1,
The container invokes this method (ejbActivate) on the instance when the container picks the instance from the pool and assigns it to a specific entity object identity. ... The instance can obtain the identity of the entity object via the getPrimaryKey(), getEJBLocalObject(), or getEJBObject() method on the entity context. ...
David Yuan
Greenhorn

Joined: Mar 01, 2004
Posts: 15
Thanks!
-David.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: set the primary key in the ejbCreate and ejbActivate methods?
 
Similar Threads
ejbLoad is not getting called
Searching problem
Problem in bmp entity bean while running client?
Exception
PreparedStatement and NullPointerException