Access Bean usage | Lazy instantiation


Well formed constructor


Non-default AccessBean constructors are usually mapped to the corresponding EJB create() method, which in turns will trigger database INSERT. For example, fooAccessBean(int p1, String p2) is mapped to the corresponding create(int p1, String p2) in the EJB Home interface.

Consider the case where a table having three columns is represented by a single EJB entity Bean (fooBean), where the first two columns are non-nullable. Assume that we have two create() methods:

Within a single transaction, the following code snippet will cause 2 SQL calls in the database as marked:

// INSERT call to database
fooBean aBean = new fooBean(3, "testing");

// UPDATE call to database
aBean.setP3(4);

While the following will only trigger one SQL call:

// INSERT call to database
fooBean aBean = new fooBean(3, "testing", 4);

Unless it is required by the logic, the second code snippet using the well-formed constructor is preferable over the first one.