Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

set the primary key in the ejbCreate and ejbActivate methods?  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
-David.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!