JBuilder9 + WebLogic7 actual articles Entity bean

xiaoxiao2021-03-06  21

V. Use EJB QL to develop inquiry

5.1 What is EJB QL

The query is often used in the operation of the relational database, mainly through the SELECT statement. Entity Bean also requires query operations as a persistent component representing data in a database, that is, an instance of Entity Bean that meets a query condition can be found. The query operation of the Entity bean is done by defining the finder () method. For CMP, define the Finder () method is merely declaring a method, indicating the parameters of the Finder () method, which is usually corresponding to the parameters in the query condition, and the actuated action is done by the EJB container. The EJB container is to read the item in the deployment description file EJB-JAR.XML (in * .jar / meta-infer), which contains query statements corresponding to the finder () method. The syndrome specification followed by Query> items is EJB QL.

The development query steps of EJB QL are as follows:

1> Increase the Finder () method in the HOME interface, which is the parameters used in the query condition;

2> Define the EJB QL statement in the item of the EJB-JAR.XML file.

EJB QL is the new feature of EJB2.0, which implements how to define various lookup methods in the HOME interface. It is based on SQL-92, which can be automatically compiled by the container, which makes the Entity Bean have higher portability and is easy to deploy.

The EJB QL statement consists of three clauses of SELECT, WHERE, ORDERBY, and thereafter two clauses are optional.

The EJB QL query statement is as follows:

example 1:

Select Stu from student as stu where stu.grade> 5

The meaning of the query statement is to query the Student Bean instance of Grade> 5. "Student" is an abstract mode name (the name specified by the item in the EJB-JAR.XML file. "Stu" is the alias of Student, the advantage of introducing alias is the field of the object representative. Stu.grade represents the GRADE field of the Student, called a path expression.

Example 2:

SELECT I from student as I where I.name =? 1

The meaning of this query statement is the STUDENT bean instance that looks for the first parameter in the Finder () method.

WHERE words instructions:

★ Input parameters of the corresponding Finder () method in? N;

★ The value of the string type should be enclosed in single quotes (if there is a single quota in the value, use the double quotation mark)

The expressions and operators that can be used in the WHERE statement are as follows:

☆ , -, *, /, =, <, <=,> =,>, <>, not, and, or

☆ between

☆ Like

☆ in

☆ MEMBER OF

☆ Is NULL (is not null)

Built-in function:

● Concat (String First, String Second)

● Substring (String Source, Int Start, Int Length)

● Locate (String Source, String Patter)

● Length (String Source)

5.2 Development Example of Query Method

5.2.1 Open the COMSample project established in "Entity Bean (1)". Double-click Cmpsample.jpx / Cmp in the project window, right-click the Student item in the EJB Designer of the Document window, select the Add / Finder menu item, create a Finder () method. 5.2.2 Set various parameters of the Finder () method.

★ Finder Name: FindByname (Finder () method name)

★ Return Type: StudENTREMOTE (Return to the type of object)

★ Input Parameter: java.lang.string name (input parameters)

★ Query: SELECT I from Student As I where I.stuname =? 1 (complete the query EJB QL statement)

5.2.3 Checking Student Entity Bean (Studient HOAVA) will find an additional Finder () method, the method is as follows:

Public Student FindByname (String Name).

5.2.4 Right-click CMPSAMPLE.JPX / CMP in the Project window and select the Rebuild command to re-package.

5.2.5 Start WebLogic Server, then right-click Cmpsample.jpx / Cmp in the Project window, select the Deploy Options for "Cmp.jar" / redeploy command to redeploy.

5.2.6 Creating a client program StudentTClient2.java via EJB Test Client Wizard, because EJB has changed, so you can't use the previously created client program. Modify the main () method is as follows:

Public static void main (string [] args)

{

StudentTestClient2 Client = New StudientTestClient2 ();

Client.studEntremote = Client.FindByname ("student2");

System.out.println ("THE Student2's ID IS" Client.getStuid ());

}

This method looks for the entity bean instance named Student2 through the FindByname () method, and then outputs the STUID value corresponding to the Entity Bean.

5.2.7 Run the client program and verify the result.

For the sake of simplicity, only one parameter is used and returned is a bean instance. If the result of the query is not unique, then set the return value as a collection.

6. Establishing the relationship between Entity Bean 6.1 Entity Bean Relationship Description Since there is a certain relationship between data, the Entity Bean representing persistent data should have a corresponding relationship. For example, "Entity Bean uses (1)" Two Dataset and Locker created in Section 2.1 in Section 2.1, these two representative students and crosses are related, both are associated with LockerID. Then the Entity Bean, Student and Locker representing these two tables should also reflect this relationship. The relationship between Entity Bean is done by establishing a relationship field in an Entity Bean. EJB2.0 supports one-way, bidirectional relationships, and there is a one-to-one (1: 1), one-to-one (1: N) or multi-pair (N: N), and a pair of relationships between the Entity Bean. 6.2 Entity Bean Relationship Development Example 6.2.1 Opening "Entity Bean" COMSAMPLE project established in "". Refer to 2.2.9 to establish an Entity Bean corresponding to the Locker data table. 6.2.2 Click the Locker item in the EJB Designer of the Document window to set the interface to local / remote, and other items use the default value. 6.2.3 Right-click the Locker item in the ejb design of the document window, select the Add / RELATIONSHIP menu item, point the direction arrow to the Entity Bean Student to establish the relationship. 6.2.4 After pointing the direction arrow to Student, you can see that a field student is added in Locker, which is a Student representing the corresponding to the Locker, called a relational field. Click the relationship field to set the properties of this relationship. The meanings are as follows: ★ RELATIONSHIP NAME: The name of the relationship. ★ MultiPlicity: Choose is one-on-one, one-to-many or more relationships. ★ Navigability: Directionality. The choice is a one-way or two-way relationship. If you select Unidirectional, you represent the selected one-way relationship, then only the relationship field representing the Student is established in the Locker. If you select Bidirectional, represent the establishment of a two-way relationship, then establish a relationship field representing the Locker in Student. ★ Cascade delete: Set whether or not the level is deleted. The cascaded deletion refers to whether to delete an Entity bean, whether it deletes the relationship. This setting is TRUE or false depends on the actual situation of the specific application. For example, there is a relationship between representing the class bean representing the Student of the class class. Because the class does not exist, its member Student does not exist, so this should be set to True. For this example, it is set to FALSE. ★ DB-CASCADE-DELETE: Similar to the meanings of Cascade delete, only the cascaded deletion of the data record represented by the Entity Bean is set. In this example, MultiPlicity is ONE to One (1: 1), NavigaBility is Unidirectional. Other items use the default value. 6.2.5 Click the Edit Rdbms Relation button in the "Relationship Properties" interface to set the associated fields of the Student and Locker data tables, ie, specify which fields are copied between the two tables.

If the two tables have the same field, such as a list of LockerID in the Student table, if there is also a field named LockerID in the Locker table, JBuilder will think that both tables are associated with the LockID field. However, the fields used in the Locker table of this example are not LockerID, but the ID, so the associated field of the data table must be manually specified. 6.2.6 Draw a straight line between the ID field and the LockerID field in the "WebLogic RDBMS RELATION Editor" interface, indicating that the two tables are associated with LockerID. 6.2.7 In the engineering window Click the Bean class to view the Locker table, you can see the code that adds an access to the relationship field: public abstract void setstudent (cmpsample.student student); public abstract cmpsample.student getstudent () The corresponding EJB-JAR.XML and WebLogic-Cmp-Rdbms-Jar.xml files are also added to the relationship. 6.2.8 Right-click the Locker item in the EJB Designer in the document window, select the Add / Method menu item to add a method, which returns the Student information associated with the Locker. Modifying the settings as follows: ★ Method name: getstuinfo ★ Return Type: String ★ Interface: Remote6.2.9 Right-click the Locker item in EJB Designer in the document window, select the Add / View Bean Source menu item, modify the getstuinfo () method As follows: public string getstuinfo () {string t = null; try {student sturemote = getStudent (); t = "student id =" sturemote.getstuid () "; student name =" sturemote.getstuname ();} Catch (Exception E) {} return t;} 6.2.10 recompile, generate a JAR file, and then use the redeploy command to deploy the newly generated CMP.jar file to WebLogic Server. 6.2.11 Establishing a client program of Locker LockertestClient1.java. 6.2.12 Modify the main () method as follows: public static void main (string [] args) {locatestclient1 client = new lockertestclient1 (); client.FindByPrimaryKey (New BigDecimal (2)); client.getstuInfo (); gen The method first looks for a StUID 2, and then call the getStuInfo () method to obtain the Locker's information corresponding to the student. 6.2.13 Run the program and verify the result.

7. Accessing the Entity Bean through the session bean, the Entity bean given in the previously passed the java client program directly accessing the entity bean (through the remote interface of the Entity Bean), which usually does not use this practice in the actual application system development. It is often used to use 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 References Accessing other EJBs in an EJB, such as Session Bean Facade To access the entity bean student, you can use the "hardcod" implementation directly in the Facade, that is, find the HOME object with the JNDI name through Stude, then get the external interface (REMOTE interface or local interface), complete method calls. 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 content it is unchanged, it is changeable, see the following code: FAAN> Student student Facade After 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 the Lookup method parameter is "java: / comp / env /" EJB reference name, For example, the EJB reference called Student, the corresponding JNDI name is "java: / comp / env / student".

7.2 EJB Reference Example 7.2.1 Opening "Entity Bean) COMSAMPLE project established in (1)" Newly built a session bean called Facade (see "session bean"). The following information is modified: ★ bean name: facade ★ interface: remote ★ session type: stateless7.2.2 Right-click FACADE in the Document window "EJB Designer", 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: Remote7.2.6 Add Accessing Student code in the FACADE BEAN class. 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.

FacadeBean.java replaced with the following Gray code Code :( hand code section is 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 CreateException {try {context context = new InitialContext (); Object ref = context.lookup ( "java: / comp / env / student") ; studentHome = (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); studenter = (studentHome) Ref;} catch (Exception E) {E.PrintStackTrace ();}} PU blic 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.

转载请注明原文地址:https://www.9cbs.com/read-44036.html

New Post(0)