This topic includes the following sections:
Where appropriate, comparisons are made between EJB factories and data classes and Java™ bean wrappers and copy helpers that still employ the original design.
To explain how to use EJB factories to instantiate enterprise beans, assume that you have an existing entity bean named MyEJB. When MyEJBAccessBean is created, the default constructor is mapped to findByPrimaryKey(MyEJBKey). MyEJBKey has a single String field named field lastName.
MyEJBAccessBean ab = new MyEJBAccessBean(); // maps to findByPrimaryKey(MyEJBKey); ab.setInitKey_lastName = "stokes"; int i = ab.getAnInt(); // <- findByPrimaryKey(MyEJBKey) occurs here ...
MyEJBFactory factory = new MyEJBFactory(); MyEJB myEjb = factory.findByPrimaryKey(new MyEJBKey("stokes")); int i = myEjb.getAnInt(); ...
MyEJBFactory factory = new MyEJBFactory(); MyEJBKey key = new MyEJBKey("stokes"); MyEJB myEjb = factory.findByPrimaryKey(key); int i = myEjb.getAnInt(); ...
MyEJBAccessBean ab = new MyEJBAccessBean("stokes"); // <- create occurs here ab.setAnInt(5); ...
MyEJBFactory factory = new MyEJBFactory(); MyEJB myEjb = factory.create("stokes"); myEjb.setAnInt(5); ...
MyEJB myEjb = new MyEJBFactory.create("stokes"); myEjb.setAnInt(5); ...
MyEJBAccessBean ab = new MyEJBAccessBean(); // maps to create(String); ab.setInit_argKey = "stokes"; int i = ab.getAnInt(); // <- create(String) occurs here ...
MyEJBFactory factory = new MyEJBFactory(); MyEJB myEjb = factory.create("stokes"); int i = myEjb.getAnInt(); ...
MyEJBAccessBean ab = new MyEJBAccessBean(); // maps to findByPrimaryKey(); ab.setInit_NameServiceTypeName("com.ibm...CNInitialContextFactory"); ab.setInit_NameServiceURLName("IIOP://9.21.35.100:900"); ab.setInitKey_lastName = "stokes"; int i = ab.getAnInt(); // <- findByPrimaryKey() occurs here ...In the new design, the same code can be written almost exactly the same way:
MyEJBFactory factory = new MyEJBFactory(); factory.setInitialContextFactoryName("com.ibm...CNInitialContextFactory"); factory.setInitialNameServiceProviderURL("IIOP://9.21.35.100:900"); MyEJB myEjb = factory.findByPrimaryKey(new MyEJBKey("stokes")); int i = myEjb.getAnInt(); ...However, if you wanted to use a home interface method other than the one mapped to the default constructor (in this case findByPrimaryKey), then in the original design, you would not be able to use the following code:
MyEJBAccessBean ab = new MyEJBAccessBean("stokes"); // <- create() occurs here // The following two lines have no effect, because the home is already acquired ab.setInit_NameServiceTypeName("com.ibm...CNInitialContextFactory"); ab.setInit_NameServiceURLName("IIOP://9.21.35.100:900"); int i = ab.getAnInt(); ...You would need to use the following code instead:
//The following two lines call static setters on the AbstractAccessBean class MyEJBAccessBean.setInit_GlobalNameServiceTypeName("com.ibm...CNInitialContextFactory"); MyEJBAccessBean.setInit_GlobalNameServiceURLName("IIOP://9.21.35.100:900"); MyEJBAccessBean ab = new MyEJBAccessBean("stokes"); // <- create() occurs here int i = ab.getAnInt(); ...
However, this code is not thread safe. If the thread is interrupted immediately before its constructor call and another thread sets these global values to something else, then the access bean will note reference the name service that you have set. In the new design, you would use the following code:
MyEJBFactory factory = new MyEJBFactory(); factory.setInitialContextFactoryName("com.ibm...WsnInitialContextFactory"); factory.setInitialNameServiceProviderURL("IIOP://9.21.35.100:900"); MyEJB myEjb = factory.create("stokes"); int i = myEjb.getAnInt(); ...
MyEJBAccessBean ab = new MyEJBAccessBean("stokes"); // <- maps to find(String) ab.setAnInt(5); ab.setAString("xxx"); ... ab.commitCopyHelper();
In the new design, the cacheable properties would be stored in a separate object; for instance, MyEJBData. The code written for the copy helper would now be written as follows:
MyEJBData data = new MyEJBData(); data.setAnInt(5); data.setAString("xxx"); ... MyEJB myEjb = new MyEJBFactory().find("stokes"); myEjb.setMyEJBData(data);
If you're using a copy helper access bean to get several properties, you might write something that resembled the following code:
MyEJBAccessBean ab = new MyEJBAccessBean("stokes"); // <- maps to find(String) int i = ab.getAnInt(); String s = ab.getAString(); ...
Under the new design, the code would be written as follows:
MyEJB myEjb = new MyEJBFactory().find("stokes"); MyEJBData data = myEjb.getMyEJBData(); int i = data.getAnInt(); String s = data.getAString(); ...
MyEJBAccessBean ab = new MyEJBAccessBean(); // maps to findByPrimaryKey(MyEJBKey); ab.setInitKey_lastName = "stokes"; try { int i = ab.getAnInt(); // <- findByPrimaryKey(MyEJBKey) occurs here ... } catch (NamingException nameExc) { ... } catch (FinderException finderExc) { ... } catch (CreateException createExc) { ... } catch (RemoteException remoteExc) { ... } // endtry
In the new design, the same code would be written as follows:
MyEJBFactory factory = new MyEJBFactory(); try { MyEJB myEjb = factory.findByPrimaryKey(new MyEJBKey("stokes")); int i = myEjb.getAnInt(); ... } catch (FinderException finderExc) { ... } catch (RemoteException remoteExc) { ... } // endtry