EJB programming & troubleshooting: Entity bean Lifecycle
I have 2 major confusions regarding the EntityEJB lifecycle:
1)I dont understand why ejbActivate is called on an instance when ejbFind has been called on an instance and it has returned a PK to the Appserver(Home object).At this stage,the instance fields are instantiated and the bean is in a method-ready state.But it seems from the EJB lifecycle diagram, that even after ejbFind has been called, the bean is still in the pool with no state associated to it,which is incorrect.
2)In an CMP, why is ejbLoad/ejbStore called after ejbFind(findByPrimaryKey)?Since the bean has just completed loading
the values from the DB,what is the purpose of calling another ejbLoad+ejbStore on the instance.Isnt this a waste of resources?
Guys,What did i miss ??
The ejbFind method does not populate the EJB with state information. The only thing the ejbFind method does is return primary keys; nothing else.
If this confuses you, think of this: an ejbFind method can also return a collection of keys, rather than one key. Ultimately those keys will be used to populate a collection of EJB with state. There is no way this can be done in the ejbFind method of the first EJB, which is why ejbFind and ejbLoad behave the way they do.
Also note that this is only an issue for BMP. CMP Entity beans tend to be much more effecient.
But then what happens in case of a BMP?...where the ejbFindByPrimaryKey method is actually used to load the fields from the DB and initialize the bean instance. After the method completes, is the bean still in the pool,even though it has a state now?
But then what happens in case of a BMP?...where the ejbFindByPrimaryKey method is actually used to load the fields from the DB and initialize the bean instance. After the method completes, is the bean still in the pool,even though it has a state now?No ejbFind method (including ejbFindByPrimaryKey) populates the fields of a bean. The correct way to implement ejbFind methods is to simply find and return the appropriate PK (or collection of PKs). No changes to bean state are necessary, and any changes that you make would be ignored anyway.