JBUILDER9 WebLogic7 actual articles
Entity Bean Application (1)
Author: Kay Huang
E_mail: hk_sz@163.com
First, the basic theory of Entity Bean
1.1 What is Entity Bean
Entity Bean is a persistent data component that represents a persistence object stored in an external medium or an existing enterprise application system resource. Simply put, an Entity Bean can record a line of rows in the database, multiple client applications can access the Entity Bean that represents the database record in a shared manner.
So what is a lasting data component? Why use persistent data components? Understand these two questions, it also clears the nature and use of Entity Bean.
The persistent data component refers to such an object that knows how to put itself in a lasting storage space. They use some persistent mechanisms such as serialization, O / R mapping. This object represents data, such as the following information using the persistent data component:
● Bank account information, such as account, password, and balance;
● Employee information, such as name, department, and salary.
Why do they handle these data in the way instead of direct processing of raw data in the database, such as related records? The answer is to treat data as an object is very convenient because it is convenient to operate and manage objects, and they behave as a compact form. In addition, through the application server where the components are located, transactions, security, etc. can be obtained.
Entity Bean is this persistent data component. Entity Bean knows how to save yourself permanently in a storage space such as a database. Entity Bean stores data in a field, such as a bank account, password, and balance.
Relying on transaction services provided by EJB containers, multiple client applications can realize sharing of data resources under the consistency and integrity of the database record. The life phase of the Entity bean is relatively long and its status is continuous. As long as the database record represented by the Entity bean exists, the component object instance has existed, even if the EJB container crashes, Entity Bean still has vitality.
1.2 Entity Bean subtype
In accordance with the implementation of Entity Bean persistence, Entity Beans can be divided into two models of container management persistence (CMP container-managed personistence) and component management persistence (BMP bean-managed persistence). In the implementation code of the CMP type EJB component, component developers do not need to write code for any database operations for component's persistent control methods, but automatically create by deployment tools during component assembly and deployment; if you want to create BMP type EJB Components, component programming needs to write control code for all persistence methods.
1.3 Entity Bean Features
Compared to the data record in the database, each entity type EJB component contains a primary key identifier, which is the same as the database record master key represented by the component. Client applications can use this primary key to locate the ENTITY bean object instance in the EJB container, and the database records represented by the component.
The main features of the entity type EJB component include:
● Entity Bean provides a view recorded in the database;
● Entity Bean has unrestricted life, and EJB server crashes will not affect the existence of Entity Bean;
● Multiple entity beans can record the same database (● EJB server can use the ENTIN's passivate method to cache the entity bean into the temporary storage space, can also re-read the cached ENTITY BEAN into the EJB container and restore component objects using the ACTIVATE method. Example;
● Client applications can be managed using creation, deletion, and query defined in the HOME interface of Entity Beans.
Second, create an Entity Bean
2.1 Preparation
First, in the Oracle9i database established tables Student and Locker, the SQL script of the table is as follows:
★ Script of the Student table:
Create Table Student (Stuid Integer Primary Key, Stuname VARCHAR (50), LockerID Integer;
Insert Into Student (Stuid, Stuname, Locker) Values (1, 'Student1', 1);
Insert Into Student (Stuid, Stuname, Locker) Values (2, 'Student2', 2);
Insert Into Student (Stuid, Stuname, Locker) Values (3, 'Student3', 3);
★ Locker table script:
Create Table Locker (ID Integer Primary Key, LockerName Varchar (50));
INSERT INTO Locker (ID, Lockername) Values (1, 'Locker1');
INSERT INTO Locker (ID, Lockername) Values (2, 'Locker2');
INSERT INTO Locker (ID, Lockername) Values (3, 'Locker3');
Before you here, please refer to the "JDBC 1) of JBuilder9 WebLogic7" (JDBC 1) "to configure the connection pool and TX data source of the database.
2.2 Creating an Entity Bean
2.2.1 Start JBuilder9, create a new project cmpsample;
2.2.2 Click Enterprise Publish;
2.2.3 Select EJB Module and then click OK to appear an EJB Module window;
2.2.4 Enter CMP after NAME;
2.2.5 Change Version to EJB 2.0 Compliant;
2.2.6 Click OK. EJB is designed in the document window.
2.2.7 Estiity Bean corresponding to the Student Table is established by introducing mode (Schema) in the database. Right-click on the document window, click the Import Schema from Database menu item;
2.2.8 Setting the database connection information in the "Database Schema Provider" interface. All settings are as follows:
★ Driver: Select Oracle.jdbc.driver.OracleDriver in the drop-down list;
★ URL: JDBC: Oracle: Thin: @localhost: 1521: KKDB
★ Username: Test
★ Password: Test
★ Database Name: KKDB
★ JNDI NAME: JDBC / TESTTXDataSource
After completing, you can see views of all tables in the database in the "Datasources" window on the left.
2.2.9 Press Right-click on Student to click the CREATE CMP 2.0 Entity Bean menu item to establish a CMP corresponding to the Student table.
2.2.10 Setting Entity Bean information
Click the Student item in the EJB Designer window to make settings.
Here, just set the interface type (interfaces) to local / transote (indicate two interfaces simultaneously), and other items can be used. The meanings are as follows:
● Bean name: Entity bean name;
● Abstract Schema Name: Abstract model name;
● Interfaces: Outer interface type, select Local, Remote or Local / Remote;
● ALWAYS WRAP PRIMARY Key: Does always protect the primary key;
● Class and packages button: Set the creation of class names and package names
○ Click on a property to set the information for this property. For example, the name of the setting attribute is set, whether it is the primary key, whether it is parameter for the EJBCREATE method. Here we default.
2.2.11 Press Right-click on the engineering window cmpsample.jpx / cmp, click the Make menu item to generate a CMP.jar file.
2.2.12 Start WebLogic Server.
2.2.13 Engineering Window Cmpsample.jpx / Cmp / Cmp.jar Press Right-click to Click the Deploy Options for Cmp.jar / Deploy menu item to deploy.
Third, the composition of Entity Bean
Entity Bean is composed of a bean class and an interface file. Interface files include external and HOME interfaces. There are two external interfaces: Remote Interface and local interface (Local Interface). The HOME interface is consistent with the external interface, ie each of the external interfaces corresponds to an HOME interface. Combined with an instance of the Entity Bean (CMP type) just created, explain the components.
3.1 Bean class
Like Session beans, the package business logic is encapsulated in the bean class. In the session bean, it is usually packaged, and the Entity bean is representative of data, so the entity bean should package data (or access to the data). Taking Student CMP as an example, the Bean class has a method of controlling the EJB lifecycle in the bean class (such as ejbcreate (), ejbremote (), EJBPOSTCREATE (), and the specific application is mainly a set of Get / set method. Each pair of GET / SET methods correspond to a field in the data table, such as a STUID field in the Student table, and there is a set of methods setstuid () and getstuid () in the Entity bean.
It is also important to note that the CMP type Entity Bean, which is an abstract class that implements the EntityBean interface. The method inside does not need to write itself, because the CMP persistence operation (such as access to the database field) is implemented by the container. If BMP is developed, the Bean class is not this structure. The bean class should be a specific class that implements the EntityBean interface, and the code to access persistence data (such as JDBC code).
The definition of the bean class of CMP Student See StudentBean.java.
3.2 Local Interface The local interface is one of the external interface, and the local interface in the Entity Bean is relatively commonly used. Because in a J2EE application, the Entity bean is usually accessed by the session bean. For EJB running in the same application server, the use of localized interfaces can greatly improve access efficiency. This interface defines the business method of EJB.
The definition of the local interface of CMP Student See Student.java.
3.3 Remote Interface (Remote Interface)
The remote interface is an external interface of EJB, defines the business method of EJB, and the remote client program is accomplished by the EJB through this interface.
Remote Interface Definition of CMP Student See StudientRemote.java.
3.4 HOME interface
The HOME interface of the Entity Bean defines the generation and query operation of the Entity Bean. Two methods that the interface must be defined is the Create () method and the FindByPrimaryKey () method. The create () method defines the operation of generating the Entity Bean, and the FindByPrimaryKey () method defines the operation of the Entity Bean in accordance with the primary key. Strictly speaking, these methods only define a rule of these operations, such as the number of parameters when generating the Entity bean, the specific implementation is done by the container.
Taking Student's Home interface as an example, the definition of the Create () method is as follows:
Public Student Create (BigDecimal Stuid) THROWS CREATEXCEPTION;
The parameter of the CREATE () method is corresponding to the primary keystock of the Student data table, because calling the create () method creates a new Entity Bean, and the corresponding record corresponding to the Entity Bean is established in the database, the STUID field recorded It is indispensable. Therefore, the Create () method actually defines an operational rule, that is, the parameters must be included with the primary key of the data table when establishing the Entity Bean.
Look at the FindByPrimaryKey () method, the definition of this method is as follows:
Public Student FindByPrimaryKey (BigDecimal Stuid) THROWS FINDEREXCEPTION;
This method defines the operation of the Entity Bean in accordance with the primary key. If there is conditional query statement in the database operation, a query operation must be defined in the home interface, and the query is the most common inquiry method according to the primary key. In addition to the FindByPrimaryKey () method, EJB developers can also define other query methods, such as queries via Stuname, but the FindByPrimaryKey () method must be defined.
CMP Student's Local Home Interface See StudhenTHome.java.
Fourth, test Entity Bean
4.1 Click the Enterprise subpage, select the EJB Test Client and click OK,
4.2 Establish a test-based client program through "EJB Test Client Wizard". Use the default setting.
4.3 Put the private context getInitialcontext () method
String URL = "T3: // Hostname: 7001";
Replace with:
String URL = "T3: // localhost: 7001";
4.4 Add the following code to the public static void main (String [] args method, complete the Student Entity Bean looking for Stuid 1, then output the corresponding Stuid, Stuname, and Lockerid:
Public static void main (string [] args)
{
StudentTestClient1 Client = new studentTestClient1 ();
Client.FindByPrimaryKey (New BigDecimal (1));
System.out.println ("stuid =" client.getstuid ());
System.out.println ("stuname =" client.getstuname ());
System.out.println ("LockeriD =" Client.getLockerId ());
}
4.5 Operating View Results.
4.6 Replacing the code just added in the public static void main (String [] args method in the following language, completes the new STUDENT BEAN.
Public static void main (string [] args)
{
StudentTestClient1 Client = new studentTestClient1 ();
Client.create (New BigDecimal (4));
Client.SetStuname ("student4");
Client.setlockerid (New BigDecimal (4));
}
4.7 Operating View Results.
appendix
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.