• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ejbPostCreate() is for what?

 
Ceppad Reso
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"An example is the use of EJB context to acquire a bean's object interface. Until the bean is fully established, there is no way for a bean to acquire its own reference" -- Howard Kushner's book.
Are there other examples? Should the relationship code be placed in this method?
For Howard Kushner's example, why session bean has no such need?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. CMR fields cannot be initialized in the ejbCreate() section; they must be done in ejbPostCreate().
 
Mikalai Zaikin
Ranch Hand
Posts: 3371
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may think of
1. ejbCreate:
a. INSERT INTO ....
b. SELECT FROM ... - after insert EJB Container get DB table primary key for created instance
2. ejbPostCreate:
a. UPDATE SET .... - EJB Container is updating relationships tables. For this EJB Container need PK he obtained in ejbCreate
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy -- the main difference between entity beans and session beans with respect to getting a reference to the EJB Object is that for an entity bean, the Container cannot prepare the EJB Object until AFTER ejbCreate() returns. Because...
* The Container needs the primary key in order to make the EJB Object.
* The Container cannot know the primary key until *after* the ejbCreate() method runs (regardless of whether its BMP or CMP).
With Session beans, though, the Container already *has* all the information it needs to make an EJB Object for a bean *before* it calls ejbCreate() on the bean instance.
So, the difference in the lifecycle, and in fact the original reason why ejbPostCreate() is part of the Entity Bean API is because the Container needs the bean to determine its own primary key, then tell the Container (by returning from ejbCreate()) and THEN the Container can *read* the primary key and create the EJB Object. But the bean still needs a chance to finish its initialization (and possibly get a reference to its own EJB Object), so that's why part of an entity bean's *creation* includes a follow-up call to ejbPostCreate().
Before CMP 2.0, this last-chance-to-finish-initializing-yourself-and-get-a-ref-to-your-own-EJBObject was really the only important reason for having ejbPostCreate() as part of the Container callback on an entity bean. But as Nathaniel and Mikalai pointed out, with CMP 2.0, ejbPostCreate() is the place where you must initialize your CMR fields.
cheers,
Kathy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic