Hi all, I'm studying for SCEA exam. Reading EJB books raised one question. In which call-back method data is actually inserted on DB ? I believed that the right answer was during "ejbPostCreate", so I have built a simple use case with WLS samples.
But it's not accurate. Data is actually inserted in "ejbStore" which is called AFTER ejbPostCreate. And it's the same both for CMP and for BMP.
Can anybody confirm this ? I have seen whiz lab exam has separate question for both CMP and BMP.....
Originally posted by Francesco Marchioni: In which call-back method data is actually inserted on DB?
BMP - ejbCreate. See Figure 26 OID of Creation of an entity object with bean-managed persistence on page 136 in the EJB 1.1 specification.
ejbStore() is called to update the data of a BMP bean. See Figure 28 OID of passivation and reactivation of an entity bean instance with bean-managed persistence on page 138.
CMP - None. The container is responsible for the insertion of the data - so it can't happen during any callback method. See Figure 27 OID of creation of an entity object with container-managed persistence on page 137 in the EJB 1.1 specificatioin. The insertion occurs after ejbCreate and before ejbPostCreate.
ejbStore() is called before the container updates the data for a CMP bean after which ejbPassivate() can be called. See Figure 29 OID of passivation and reactivation of an entity bean instance with CMP on page 139.
Francesco, why do you think ejbCreate() does not insert data? Is it because you set break point somewhere in ejb[Post]Createm watch database content with DB client and cannot see new data there? If so, keep in mind that EJB container probably would conduct severall method calls in a single tracsaction and commit it only when whole creation process successful. Otherwise transaction will be rolled back. Anyway, the "I" of ACID makes new data invisible until transaction commits.
As ejbPostCreate() called, data already exists in database, otherwise you wouldn't have been able to set CMR fields.
Originally posted by Pavel Grushetzky: Francesco, why do you think ejbCreate() does not insert data?
Part of the problem is identifying whether you are talking about a Bean Managed Perstence (BMP) Entity Bean or a Container Managed Persistence (CMP) Entity Bean. This is exasperated by the fact that SCBCD study guides only discuss CMP entity beans (because that what the SCBCD exam covers). Compounding the problem is the fact that entity beans can only use Container Managed Transactions (CMT) which sometimes confuses people into thinking that Entity Beans can only be CMP.
Bottomline: In a BMP Entity Bean the bean provider is responsible for inserting the DB record in the ejbCreate. In a CMP Entity Bean the bean provider is responsible for setting the bean fields that compose the primary key (and presumably any other "not-null" constrained fields) in the ejbCreate - so that after the ejbCreate the container can use the values in those fields to issue the necessary "insert" statement.
Hey! Wanna see my flashlight? It looks like this tiny ad: