JBuilder9 + WebLogic7 actual articles Entity bean is used (3)

zhaozj2021-02-16  58

JBUILDER9 WebLogic7 actual articles

Entity Bean Application (3)

Author: Kay Huang

E_mail: hk_sz@163.com

6. Establish an Entity Bean relationship

6.1 Description of the relationship of Entity Bean

Since there is a certain relationship between the data, the Entity Bean representing persistence data should also 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-fold (N: N).

6.2 Entity Bean Relationship Development Example

6.2.1 Open "Entity Bean" COMSAMPLE project established in (1) ". 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 that 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.

Modify the settings of this method as follows:

★ Method name: getstuinfo

★ Return Type: String

★ Interface: Remote

6.2.9 Right-click the Locker item in the EJB Designer in the document window, select the Add / View Bean Source menu item, modify the getstuinfo () method:

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 Re-build, generate the JAR file, and then deploy the newly generated CMP.jar file to WebLogic Server using the redeploy command.

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)

{

LockertestClient1 Client = New LockertestClient1 ();

Client.FindByPrimaryKey (New BigDecimal (2));

Client.getstuinfo ();

}

This method first looks on the Student of StuID 2 and then calls the getStuInfo () method to obtain the information of the Locker corresponding to the Student.

6.2.13 Run the program and verify the result.

reference

Electronic Industry Press "J2EE App Development (JBuilder WebLogic"

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

New Post(0)