Java bean wrappers and copy helpers

You can use a Java bean wrapper or copy helper access bean that typifies the original access bean design to create a client application.

This process involves two key steps:

The following code example helps to explain these two steps:

EmployeeAccessBean aEmployee = new EmployeeAccessBean()
aEmployee.setInit_employeeNo ("100");
aEmployee.setName ("IBM");
aEmployee.setAddress ("1150 Eglinton Ave, Toronto");

If the client program sets copy helper attributes in an access bean and performs a get operation before calling commitCopyHelper(), then the access bean will return the data in the cache rather than the data in the enterprise bean. Generally, if an attribute resides in the cache, the access bean will get it from the cache. However, if you perform a get operation on a copy helper field that has not been set or retrieved, the cache will be refreshed from the enterprise bean.

If you are going to call a finder that returns an enumeration and you invoke it from a client program outside of a transactional scope, only the first five results are returned in the enumeration. To return all of the results, you need to make sure that the finder method is invoked within a transaction. To do this, you can either call the finder method from a session bean method, where the method has a container-managed transaction attribute, or you can create a user transaction in your client. For example:

// Get the initial context
java.util.Properties p = new java.util.Properties();
p.put(Context.PROVIDER_URL, "IIOP:///");
p.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
InitialContext initContext = new InitialContext(p);
// Look up a transaction
userTran = (UserTransaction)initContext.lookup("jta/usertransaction");
userTran.begin();
// Call the finder
// Assume that employeeHome has already been found, and has a method defined findAll()
Enumeration enum = employeeHome.findAll();
while (enum.hasMoreElements()) {
// Process the enumeration
}
userTran.commit();

 

Parent topic

EJB access beans and client applications