JBUILDER9 WebLogic7 actual articles
Application of Entity Bean (4)
Author: Kay Huang
E_mail: hk_sz@163.com
Seven, access Entity Bean through session bean
The Entity Bean instance given in the previous is through the Java client program directly accessing the entity bean (remote interface through the Entity Bean), which usually does not use this practice in the actual application system development, which is often used in this way. : Create a session bean that encapsulates access to Entity Beans in the session bean. And when accessing the local interface of the Entity Bean, the client does not deal with the entity bean directly, and the client completes the operation of the Entity Bean through the Session Bean. This practice is actually following a design pattern: Facade design mode.
7.1 EJB reference
Accessing other EJBs in an EJB, such as Session Bean Facade To access Entity Bean Student, you can use the "hardcod" implementation directly in FACADE, that is, find Home objects through Student's JNDI name, then get an external interface (Remote interface) Or Local interface), complete method call. The disadvantage of this method is obvious because the two EJBs are tightly tiered together, such as what changes, such as STUDENT, such as change names, then Facade must change code (this is why "Entity Bean (2) "The reason for re-establishing a TestClient).
J2EE uses such a way to resolve this problem: Define an EJB reference in the Facade deployment description file, which describes the reference to another EJB, only using the name of the EJB reference in the FACADE code, the name of the EJB reference is The same, but what it references is variable, see the following code:
ejb-reason-descriptor>
reference-descriptor>
WebLogic-Enterprise-Bean>
After establishing the EJB reference, Facade can get the interface to access the EJB Student interface, see the following code:
Context context = new initialContext ();
Object ref = context.lookup ("java: / comp / env / student");
StudENTHOME = (studentHome) REF;
When using the EJB reference, the complete JNDI name as a lookup method parameter is "java: / comp / env /" EJB reference name, such as the EJB reference called Student, corresponding to the complete JNDI name is "Java: / COMP / Env / student. 7.2 EJB Reference Example
7.2.1 Opening "Entity Bean" COMSAMPLE project created in "Entity Bean, newly built a session bean called Facade (see" session bean "). Modify the following information from this bean:
★ Bean name: facade
★ Interface: Remote
★ Session Type: Stateless
7.2.2 Right-click FACADE in the Document Window "EJB Designer" and select the Open DD Editor menu item.
7.2.3 Click the EJB Local References tab below in the DD Editor interface.
7.2.4 In the Settings interface of "EJB Local References", click the Add button, then enter the name Student referenced by the EJB reference after the NAME item, select the IsLink check box (if the EJB to reference is in the same jbuilder engineering) This item), the LINK item selection student (EJB to reference), and other items can be set with the system automatically.
7.2.5 Add a method getStuInfo () in FACADE, the functionality of this method is to find the Entity Bean Student corresponding to the parameter (representing stuid), then call the Student method getStuid () and getStuname (). The properties of this method are as follows:
★ Method name: getstuinfo
★ Return Type: String
★ Input Parameters: String Stuid
★ Interface: Remote
7.2.6 Join the Accessing Student code in the BEAN class of the Facade. To access Student, first get the Home object, which can be obtained by eJB reference. Getting the HOME object This part of the code is placed in the EJBCREATE () method of the Facade () (ie, as long as the instance of Facade is established or activated, you will get the Home object). After obtaining the Home object, you get the local interface (Local Interface) of Student through the Home object in the getStuInfo () method, and then call the Student method by the local interface. Replace the code of FacadeBean.java with the following code: (Gray code part is manually added)
Package cmpsample;
Import java.math. *;
Import javax.ejb. *;
Import javax.naming. *;
Public Class Facadebean
Implements sessionbean
{
SessionContext sessionContext;
Public student student = null;
Private studenthome studenthome = NULL;
Public void ejbcreate () THROWS CREATEXCEPTION
{
Try
{
Context context = new initialContext ();
Object Ref = context.lookup ("java: / comp / env / student); studenter = (studentHome) Ref;
}
Catch (Exception E)
{
E.PrintStackTrace ();
}
}
Public void ejbremove ()
{
/ ** @ Todo Complete this method * /
}
Public void ejbactivate ()
{
Try
{
Context context = new initialContext ();
Object ref = context.lookup ("java: / comp / env / student");
StudENTHOME = (studentHome) REF;
}
Catch (Exception E)
{
E.PrintStackTrace ();
}
}
Public void ejbpassiVate ()
{
/ ** @ Todo Complete this method * /
}
Public void setsessionContext (sessionContext sessioncontext)
{
This.SessionContext = sessionContext;
}
Public String getstuinfo (String Stuid)
{
String strstu = null;
Try
{
Student = studentHome.FindByPrimaryKey (New BigDecimal (stuid));
Strstu = "the studentid is" student.getstuid () "name is"
Student.getStuname ();
}
Catch (Exception E)
{}
Return strStu;
}
}
7.2.7 Re-build EJB Module, generate a CMP.jar file and deploy it to WebLogic Server.
7.2.8 New FACADE client program FacadeTestClient1.java, using the client program to access the getStuInfo () method of Facade. The main () method of modifying the client program is as follows:
Public static void main (string [] args)
{
FacadetestClient1 Client = new facadetestclient1 ();
Client.create ();
Client.getstuInfo ("2");
}
7.2.9 Compiling the client program and verify the result.
reference
Electronic Industry Press "J2EE App Development (JBuilder WebLogic"
My article is the first Auro Forum (www.newer.com.cn/bbs) and programmers forum (www.9cbs.net), welcome to reprint, but please keep the author and the name of the revision, thank you.