Outline:
==========================================
I. Client Interface
1.1 remote interface
1.2 home interface
Ii. Instances
2.1 BMP Entity Bean
2.1.1 ejbcreate () method
2.1.2 ejbpostcreate () method
2.1.3 ejbremove () method
2.1.4 ejbload () and ejbstore () Methods
2.1.5 finder Method
2.1.6 Business Methods
2.1.7 database call
2.2 home interface
2.3 remote interface
2.4 deploy the descriptor
2.5 customer Program
2.6 deployment and operation
==========================================
Body:
==========================================
I. Client Interface
In J2EE component development: entity EJB (I), we understand the characteristics, usage, and two persistence types of entity EJB. The construction and usage of the home and remote client interfaces of the Entity Bean are similar to that of the Session Bean. In fact, in addition to adding a finder Method to the home Interface Definition of the object bean, there are only semantic nuances. Next, let's take a look at how to construct these interfaces of the object bean and how the client program accesses these interfaces.
1.1 remote interface
The remote interface of the Entity Bean encapsulates the Entity Bean seen by the client program. Its construction method is the same as that of the remote interface of the Session Bean. The object bean interface generally contains get and set methods, which are used to extract and set data respectively. The remote interface can also contain interface definitions at any application layer.
The following describes how to construct and use the remote interface of the object bean. Figure 1 shows the basic architecture involved in constructing a remote EJB interface.
The javax. EJB. ejbobject interface must be extended for the remote interfaces of all distributed EJB components than the displayed myentityejb. Just like session beans, entity beans use underlying stub programs, skeleton programs, and management services provided by containers, implements distributed and manageable access from client interfaces to server-side EJB components. In fact, for the Entity Bean and Session Bean, the real difference between the remote interfaces is only some semantic nuances.
Just as session beans have remote interfaces, each entity bean component must have a remote EJB interface. This interface provides a distributed interface for the client program of the Entity Bean, so that the client program can call the application layer logic of the Entity Bean. For the Distributed Application Layer Method on every entity bean component, such as myentityejbean. somemethod (), the remote interface of the EJB client must define a corresponding application layer method, such as myentityejb. somemethod (). A incidental impact of distributed features is that each method in the remote interface of the application layer must declare that it can throw java. RMI. RemoteException. Of course, this rule is only effective for methods that require distributed services on server components. In addition to the application-layer methods on the remote EJB interface, there is also a set of methods inherited from ejbobject on the remote entity bean object for calling.
1.2 home interface
The customer program of the Entity Bean creates, finds, or removes the Entity Bean through the home interface. In fact, creating an Entity Bean inserts a new data unit into the data source (for example, inserting a new row into the database table ). The interface used to find the object bean provides a mechanism to find data units, and the returned results conform to the object-oriented style (Object bean object ). Remove an Entity Bean to delete the corresponding data unit from the database. In this section, we will learn how to create and use the Entity Bean home interface, how to perform these basic operations, and other auxiliary operations.
Figure 2 shows the basic architecture involved in constructing the bean home interface and how the client program uses these interfaces. To obtain the application-layer EJB home interface object, myentityhome (which must be derived from the standard javax. EJB. ejbhome Interface) is displayed in step 2. We only need to use JNDI to find the named home reference. The client stub program of the Entity Bean implements the application layer home interface of the Entity Bean instance. on the server side, the skeleton program and the container are responsible for ing calls from the stub program.
The client program looks for the object bean home object in the same way as the session bean home object. Regardless of whether the client program runs in a J2EE container or runs independently outside the container, JNDI is used. If the client program runs in the J2EE container, you can use Element references the EJB home interface. If the entity bean acts as a client program for another EJB, you can Element Definition Element.
The home interface of the object bean defines one or more create (...) methods. These methods represent various ways to create data units corresponding to the object bean. For each ejbcreate (...) method in the object Bean class, the home interface must define a create (...) method. Create (...) the method can contain zero or multiple input parameters, and the corresponding ejbcreate (...) the initialization parameter types required by the method match, but these create (...) method returns an instance of the remote interface of the object Bean (for example, myentityejb ). In addition, the CREATE (...) method must be able to throw java. RMI. RemoteException and javax. EJB. createexception exceptions.
The create () method is not the only application-layer method that can be defined on the bean home interface. The home interface can also define a set of findxxx (...) methods. The client program queries the object bean through this set of methods. For each ejbfindxxx (...) method defined by the object Bean class, the object bean home interface must have a corresponding findxxx (...) method. For CMP object beans, the valid findxxx (...) can be determined from the deployment descriptor of the object bean because the explicit ejbfindxxx (...) method does not exist in the bean class. Each findxxx (...) method defined on the object bean home interface should also declare that the RemoteException and finderexception can be thrown to the client program.
The Entity Bean must define an ejbfindbyprimarykey () method, so the home interface must also define at least one findbyprimarykey () method. Findbyprimarykey () must return the handle of a remote object (for example, myentityejb) of an entity bean ). The container is responsible for associating the primary key returned by the ejbfindbyprimarykey () method to a specific bean instance and the returned Entity Bean client remote interface stub program. The definitions of other findxxx (...) methods must also match the corresponding ejbfindxxx (...) method. The two must have the same input and output parameters. The difference between the client is that, from findxxx (...) the enumeration or collection object returned by the method contains the implementation of the Remote Object of the Entity Bean. On the server-side Entity Bean component, the corresponding ejbfindxxx (...) the collection returned by the method contains the primary key.
Ii. Instances
In this example, a bean manages Persistent Object beans, which is a simple bean describing wage levels. The State information of the object bean is stored in the paytable table of the relational database. The structure of the paytable table is as follows:
Create Table paytable (empname varchar (10 ), Payrate real ); |
Here, empname indicates the employee name, And payrate indicates the salary grade. Empname is the primary key.
The Entity Bean class, home interface, and remote interface are implemented in payejb. Java, payhome. Java, and pay. Java respectively. The client program is payclient. java.
2.1 BMP Entity Bean
The payejb Entity Bean class meets the following requirements:
- Implement the entitybean interface.
- Bean class is defined as public.
- Bean classes cannot be defined as abstract or final.
- Implement zero or multiple ejbcreate () methods and ejbpostcreate () methods.
- Implement the ejbfindxxx (...) method.
- Business Method.
- The finalize () method cannot be implemented.
2.1.1 ejbcreate () method
As mentioned above, when the customer program calls the CREATE () method, the EJB container calls the corresponding ejbcreate () method. Payejb has only one ejbcreate () method, which completes the following tasks:
- Insert the state information of the Entity Bean into the paytable table.
- Initialize instance variables.
- Returns the primary key.
The Code is as follows:
Public String ejbcreate (string empname, float payrate) throws createexception {
If (empname = NULL ){ Throw new createexception ("employee name is required ."); } Try { String sqlstmt = "insert into paytable values (? ,? )"; Con = Ds. getconnection (); Preparedstatement stmt = con. preparestatement (sqlstmt ); Stmt. setstring (1, empname ); Stmt. setfloat (2, payrate ); Stmt.exe cuteupdate (); Stmt. Close (); } Catch (sqlexception sqle ){ Throw new ejbexception (sqle ); } Finally { Try { If (con! = NULL ){ Con. Close (); } } Catch (sqlexception sqle ){} } This. empname = empname; This. payrate = payrate; Return empname; }
|
When compiling the ejbcreate () method of the Entity Bean, pay attention to the following points:
- The access control modifier must be public.
- The return value type must be a primary key (only for bean management persistence ).
- The parameter value must be a valid Java RMI type.
- Methods cannot have final or static modifiers.
It is worth noting that non-J2EE applications can directly Insert the state information of the Entity Bean to the database. For example, you can use SQL commands to directly insert records into the paytable table. Although the Entity Bean corresponding to this record is not created by the ejbcreate () method, the client program can find this entity bean.
2.1.2 ejbpostcreate () method
After the EJB container calls the ejbcreate () method, it then calls the ejbpostcreate () method. Unlike the ejbcreate () method, the ejbpostcreate () method can call the getprimarykey () and getejbobject () methods defined by the entitycontext interface. The ejbpostcreate () method can usually be empty. For example, the ejbpostcreate () method of payejb is empty.
The ejbpostcreate () method must meet the following requirements:
- The number and type of parameters must match the corresponding ejbcreate () method.
- The access control modifier must be public.
- The final and static method modifiers are not allowed.
- The return value type must be void.
2.1.3 ejbremove () method
The client calls the remove () method to remove the Entity Bean. This call causes the EJB container to call the ejbremove () method and the ejbremove () method to delete the state information of the Entity Bean from the database. The ejbremove () method of payejb is as follows:
Public void ejbremove (){ Try { String sqlstmt = "delete from paytable where empname =? "; Con = Ds. getconnection (); Preparedstatement stmt = con. preparestatement (sqlstmt );
Stmt. setstring (1, empname ); Stmt.exe cuteupdate (); Stmt. Close ();
} Catch (sqlexception sqle ){ Throw new ejbexception (sqle ); } Finally { Try { If (con! = NULL ){ Con. Close (); } } Catch (sqlexception sqle ){} } } |
2.1.4 ejbload () and ejbstore () Methods
In the payejb class, the ejbload () method first constructs an SQL select command, then runs the SQL command to read the wage level information from the database, and saves it to the instance variable. The ejbstore () method constructs an SQL update command and then runs the update command to update data in the database. See the implementation code in payejb. java.
2.1.5 finder Method
Payejb defines two finder methods:
Public String ejbfindbyprimarykey (string primarykey) Public Collection ejbfindinrange (float lowerlimit, float upperlimit)
|
The first method returns qualified employees based on the primary key. The second method returns a set of qualified employees based on the specified salary level range. The following shows the implementation code of the second method:
Public Collection ejbfindinrange (float lowerlimit, float upperlimit) Throws finderexception {
Try { String sqlstmt = "select empname from paytable" + "Where payrate? And? "; Con = Ds. getconnection (); Preparedstatement stmt = con. preparestatement (sqlstmt );
Stmt. setfloat (1, lowerlimit ); Stmt. setfloat (2, upperlimit ); Resultset rs = stmt.exe cutequery ();
Arraylist list = new arraylist (); While (Rs. Next ()){ String id = Rs. getstring (1 ); List. Add (ID ); }Stmt. Close (); Return list;
} Catch (sqlexception sqle ){ Throw new ejbexception (sqle ); } Finally { Try { If (con! = NULL ){ Con. Close (); } } Catch (sqlexception sqle ){} } }
|
For BMP object beans, the finder method must meet the following requirements:
- The ejbfindbyprimarykey () method must be implemented.
- The method name must be prefixed with ejbfind.
- The access control modifier must be public.
- The modifier of the method cannot be final or static.
- The parameter and return value must be valid Java RMI types.
- The return value type must be a set of primary keys or primary keys.
2.1.6 Business Methods
Payejb defines two simple business methods: setpayrate and getpayrate (), which are used to set and obtain the salary level respectively.
2.1.7 database call
The following table summarizes the database access types of each method in payejb:
Method |
Corresponding SQL call |
Ejbcreate |
Insert |
Ejbfindbyprimarykey |
Select |
Ejbfindinrange |
Select |
Ejbload |
Select |
Ejbremove |
Delete |
Ejbstore |
Update |
In payejb, the Business Method finally calls the database through the ejbload () method and ejbstore () method, so the table above does not show the database call type corresponding to the business method.
2.2 home interface
The customer program creates and searches for entity beans through the home interface. The payhome interface is defined as follows:
Import java. util. collection; Import java. RMI. RemoteException; Import javax. EJB .*;
Public interface payhome extends ejbhome {
Public pay create (string empname, float payrate) Throws RemoteException, createexception; Public pay findbyprimarykey (string primarykey) throws finderexception, RemoteException; Public Collection findinrange (float lowerlimit, float upperlimit) Throws finderexception, RemoteException; } |
2.3 remote interface
The remote interface extends javax. EJB. ejbobject and defines the business methods that can be called by the client program. The remote interface of payejb is defined as follows:
Import javax. EJB. ejbobject; Import java. RMI. RemoteException;
Public interface pay extends ejbobject { Public void setpayrate (float payrate) throws RemoteException; Public float getpayrate () throws RemoteException; } |
Each method in the remote interface must match a method in the EJB class. The method parameters and return values must be valid RMI types. In addition, the throws of the method must contain java. RMI. RemoteException.
2.4 deploy the descriptor
Before deploying ejbs, you should encapsulate ejbs into jar files. Each EJB module (a jar file containing the EJB component) must contain a ejb-jar.xml deployment descriptor. Although we can usually use GUI tools to write ejb-jar.xml deployment descriptors by filling in the blanks, it is still necessary to understand the ejb-jar.xml. See the example in the download code in this article.
2.5 customer Program
The customer program payclient. Java first inserts four records into the paytable table, then searches for records whose employee name is "Sun Wukong" and outputs their salary levels. Next, the customer program modifies the employee's salary grade. Finally, the customer program looks for employees with a salary grade of 5.0 to 20.0 and outputs the list. The following is the running result of payclient. java.
2.6 deployment and operation
Next, let's take a look at how to run this application on Sun J2EE SDK, cloudscape database, and Windows 2000. The cloudscape database can be downloaded along with the J2EE SDK.
First, run the J2EE command to start the J2EE server. Then, start the cloudscape database server from the command line:
In another command window, run cloudscape-iSQL To Go To The cloudscape console and run the SQL CREATE command to create a paytable table.
In the following description, we assume that the application has been encapsulated into the ear file payapp. Ear. For more information about J2EE application encapsulation and deployment, see the development platform documentation.
Start deploytool of J2EE SDK, and select "file-> open" to open the payapp. Ear file. Next, choose tools> deploy to deploy the application. When the deployment prompt appears, select the "Return client jar" check box.
In a command window, enter the directory of the ear file (payappclient. jar file) and set the environment variable appcpath to payappclient. jar. Then, execute the following command:
Runclient-client payapp. Ear-name payappclient-textauth |
In the logon prompt, enter the username J2EE and the password J2EE. For the output of the customer program, see Figure 3.
Download the code in this article: J2eeentityejb2_code.zip