Develop CMP2.0 using Eclipse

zhaozj2021-02-16  47

Develop CMP2.0 using Eclipse

- Integrated JBoss-IDE 1.1.0, JuniteJB, MySQL

Eclipse as one of the successful open source IDEs is loved by many Java developers, but it is not directly supported by EJB development. It has been a difficulty of developers, and greatly adds EJB beginners to develop EJB difficulty; JBOSS As the most successful EJB container, due to the lack of documentation, beginners often can't catch the point, and the mistakes generated during deployment and commissioning are often helpless. This article uses the Eclipse plug-in JBoss-IDE 1.1.0 provided by JBoss Group for CMP development, hoping to lead everyone to develop ECLIPSE to develop EJB mystery.

Sample development software:

(1) Eclipse2.1 (http://www.eclipse.org)

(2) JBoss3.07 (http://sourceforge.net/projects/jboss/)

(3) JBoss-IDE 1.1.0 (http://sourceforge.net/projects/jboss/)

(4) junit (http://sourceforge.net/projects/junit/)

(5) junitejb (http://sourceforge.net/projects/junitejb/)

(6) MySQL 3.23.56 (http://www.mysql.com)

(7) MySQL JDBC 2.0.14 (http://www.mysql.com/downloads/)

One. Configuration 1. The Eclipse plugin is configured to directly decompress the downloaded jbosside file org.jboss.ide.eclipse_1.1.0.bin.dist.zip Under the directory of Eclipse2.1, the Eclipse directory structure is shown below:

If the operation is correct, the "Confirm Folder Replace Window" will appear and click "All". When the Eclipse is started next time, the plugin will be loaded automatically.

2. Configuration of JBoss3.07

There are two places where JBoss is configured, one is a log information option for JBoss, so that we can get detailed information about deployment in the deployment of EJB; the other is the data source in JBoss, so that we can use EntityBeans managed by container persistence to operate data in the database.

(1) First we describe the changes to the log information option, enter the server / default / conf directory in the directory where JBoss is located, find the file log4j.xml, open it with the text editor, modify the property as follows:

Modify one:

->

change into:

Modify 2:

change into:

After modifying these properties, when we deploy CMP EntityBean, you will see the SQL plug-in statement that is automatically generated for the deployed CMP EntityBean and the EJB-QL statement included in the CMP EntityBean; check sentence.

(2) Next, we tell the configuration of the data source. Step 1: We entered the DOCS / EXAMPLES / JCA directory under the JBoss folder, open mysql-service.xml with the text editor, and modify the part of them:

mysqlds (Data Source Name)

Name = "ConnectionURL" type = "java.lang.string"> JDBC: mysql: // localhost: 3306 / ResultManager

(Database URL)

Name = "driverclass" type = "java.lang.string"> com.mysql.jdbc.driver

(Database driver)

Name = "Username" type = "java.lang.string"> Database (database user)

Name = "Password" type = "java.lang.string">

Sailing (Database User Password)

After saving the modified content, copy the mysql-service.xml file to the JBoss folder, the server / default / deploy directory, at which time the file in the deploy directory is shown below:

In the second step, we will copy the JAR file containing the MySQL JDBC 2.0.14 driver to the Server / Default / Lib directory of the JBoss installation directory. At this time, the files in the lib directory are shown below:

Now we have completed the previous configuration, and we will tell the development of CMP EntityBean.

Two: development articles

1. Program introduction: The program contains two EntityBeans, which are StudientBean and ResultBean, respectively. StudentBean contains basic information: student_id (student number, primary key), name (name); Resultbean contains students' grade information: Result_ID (number of grades, primary key), CatalogName (subject name), score (score), Student_ID_FK (foreign key associated with the StudentBean). The real relationship diagram is as follows:

The database established script is as follows:

#

# Table structure for table 'result'

#

Create Table `Result` (

`Result_id` int (5) unsigned not null default '0',

`Catalogname` VARCHAR (50) Not null default '0',

`score` int (5) unsigned not null default '0',

`student_id_fk` int (5) unsigned default '0', primary key (` result_id`)

) TYPE = MyISAM;

#

# Table strunture for table 'student'

#

Create Table `Student` (

`student_id` int (5) unsigned not null default '0',

`Name` VARCHAR (50) Not null default '0',

PRIMARY key (`student_id`)

) TYPE = MyISAM;

2. Establish an engineering: Select FileànewàProject ..., a dialog box, as shown in the figure:

Select Java Project, click Next button, the dialog box appears:

Filling the project name ResultManager in Project Name, the following dialog box is the deployment path of the project file, select the default. After filling out, click Next button, the figure below appears:

Then select the Libraries tag in the upper right, as shown in the figure:

Select the ADD EXTERNAL JARS ... button of the right to select all JAR files in the client directory in the jboss directory, join the current project:

Click the Finish button to end the establishment of the project.

The establishment of the engineering structure is as follows (the map relating to the engineering structure of the engineering structure does not include the import engineering JAR file):

3. Establish StudentBean EntityBean: (1) Select Fileànewàclass and fill in the following figure in the pop-up dialog box:

Select Finish after completion. Eclipse will automatically generate the default implementation code for us to implement the method in the interface. The structure of the project folder at this time is shown in the figure:

(2) Write StudentBean EntityBean: Since the EJB and JBoss tags in the xdoclet have good self-explanatory, it is easy to see the meaning of its representative (due to this paper mainly telling the tool usage, the provision of CMP principle and XDoclet Introduction to the label). The StudentBean code is as follows:

Package edu.njut.resultmanager.ejb;

Import java.rmi.remoteexception;

Import javax.ejb.ejbexception;

Import javax.ejb.entitybean;

Import javax.ejb.entityContext;

Import javax.ejb.removeexception;

/ **

* @Author Sailing

* @ ejb.bean

* Name = "student"

* Display-name = "student EntityBean"

* Description = "Student CMP EntityBean"

* View-type = "BOTH"

* JNDI-Name = "student"

* Local-jndi-name = "studentlocal"

* Type = "cmp" * cmp-version = "2.x"

* Primkey-Field = "Student_ID"

*

* @ Ejb.Persistence

* Table-name = "student"

*

* @ jboss.Persistence

* DataSource = "java: / mysqlds"

* DataSource-mapping = "mysql"

*

* @ ejb.finder

* Query = "SELECT OBJECT (O) from Student O"

* Result-type-mapping = "local"

* Signature = "java.util.collection Findall ()"

*

* @ ejb.finder

* Query = "SELECT DISTINCT OBJECT (O) from student o where. =? 1"

* Result-type-mapping = "local"

* Signature = "java.util.collection findbyname (java.lang.string name)"

*

* /

Public Abstract Class StudentBean Implements EntityBean {

Private javax.ejb.entityContext_ENTINTETEXT;

// ------ CMP Fields ------

/ **

* @ Ejb.Persistence

* Column-name = "student_id"

*

* @ ejb.interface-method

* View-type = "BOTH"

* @ ejb.pk-field

* /

Public Abstract Java.lang.integer getstudent_id ();

/ **

* SET Student's ID

* @Param ID Student's ID

* /

Public Abstract void setstudent_id (java.lang.integer id);

/ **

* @ Ejb.Persistence

* Column-name = "name"

*

* @ ejb.interface-method

* View-type = "BOTH"

* /

Public abstract java.lang.string getname ();

/ **

* Set Student's Name

* @Param Name Student's Name

* /

Public Abstract void setName (java.lang.string name);

// ------ CMR Fields ------

/ **

* @ ejb.relation

* Name = "student-result"

* Role-name = "student-Has-result" * target-ejb = "result"

* Target-multiple = "no"

* Target-role-name = "result -belong-student"

* Target-cascade-delete = "yes"

*

* @ jboss.target-limited

* Related-pk-field = "student_id"

* Fk-color = "student_id_fk"

* Fk-constraint = "yes"

* /

Public Abstract Java.util.collection getResults ();

/ **

* @ ejb.interface-method

* View-type = "BOTH"

* @Param Results Student's Results

* /

Public Abstract Void SetResults (Java.util.collection Results);

// ------ EJB CREATEMETHOD ------

/ **

* @ ejb.create-method

* /

Public Java.lang.integer ejbcreate (java.lang.integer studentiD,

Java.lang.string name) throws javax.ejb.createException {

SetStudent_ID (StudentID);

SetName (Name);

// EJB 2.0 Spec Says Return Null for CMP EJBCREATE METHODS.

Return NULL;

}

Public void ejbpostcreate (java.lang.integer studentiD, java.lang.string name) {}

// ----------- EJB Callback Method -------------

/ * (non-javadoc)

* @see javax.ejb.entitybean # ejbactivate ()

* /

Public void ejbactivate () throws ejbexception, remoteexception {

}

/ * (non-javadoc)

* @see javax.ejb.entitybean # ejbload ()

* /

Public void ejbload () THROWS EJBEXCEPTION, RemoteException {

}

/ * (non-javadoc)

* @see javax.ejb.entitybean # ejbpassivate ()

* /

Public void ejbpassivate () THROWS EJBEXCEPTION, RemoteException {

}

/ * (non-javadoc)

* @see javax.ejb.entitybean # ejbremove ()

* /

Public void ejbremove ()

THROWS RemoveException, EJBEXCEPTION, RemoteException {

}

/ * (non-javadoc)

* @see javax.ejb.entitybean # ejbstore () * /

Public void ejbstore () THROWS EJBEXCEPTION, RemoteException {

}

/ * (non-javadoc)

* @see javax.ejb.entitybean # setentityContext (javax.ejb.entityContext)

* /

Public void setentityContext (entityContext Arg0)

Throws ejbexception, remoteException {

}

/ * (non-javadoc)

* @see javax.ejb.entitybean # unsetentityContext ()

* /

Public void unsentityContext () THROWS EJBEXCEPTION, RemoteException {

}

}

Note: Follow the JBoss-IDE description documentation, pressing Ctrl Space in the Java Editor to write a prompt to write the xdoclet tag, but in the Chinese version of the operating system, Ctrl Space is switched in English input method, here is generated here. The conflict of shortcuts. In order to solve this problem, we must modify the shortcuts in Eclipse. The method is: windowsàpreferenceàworkbechàkeyspen, select Ctrl Space [conflict], change Ctrl Space in the key sequence box to Ctrl Alt Space, so you can quickly complete the shortcut Ctrl Alt Space in the Java editor The label is written. After the modification is complete, as shown below:

4. Write the remaining class file: Since the remodeling of the remaining class file is not listed here, the specific code can be found in the source code of the document. When all files are written, the project's directory structure is shown below:

Note that there is an error in the code of SequenceSessionBean, which is due to the SEQUENCESessionBean called the local object (SEQUENCELOCALHOME) that is called by XDocletionBean, which automatically generates the corresponding bean file by XDoclet. The error after each related class file will disappear.

5. Use the XDoclet to generate a deployment descriptor and related class files: (1) Right-click the project name, select the Properties option in the pop-up menu, as shown:

Select the xdoclet configurations option in the pop-up dialog box

(2) Right-click in the blank area below define the xdoclet configurations, and select Add in the pop-up menu.

Fill in EJB in the pop-up dialog (name can be filled in casually).

(3) Under the "EJB" that is just added, right-click the blank area below, select the Add Doclet option in the pop-up menu.

Select EJBDoclet in the pop-up menu, click OK.

Move the properties in the EJBDoclet to modify:

(4) Select the EJBDoclet, click the right mouse button, select Add in the pop-up menu, then select FileSet.

Modify the properties of the FileSet as follows

(5) Add deploymentDescriptor with the same method, make its properties as follows

(6) Add JBOSs in the same way, modify its property as follows:

(6) Add packagesubstitution in the same way, modify the property as follows:

The package attribute here is the name of the package that will be replaced, and substerutewith is the replaced name. For example, edu.njut.ResultManager.ejb will be replaced with edu.njut.ResultManager.Interfaces,

That is, the related class files generated by the XDCOLET generated by the labels included in the class file in edu.njut.ResultManager.eb, will be stored under edu.njut.ResultManager.Interfaces.

(7) Add HOMEINTERFACE, RemoteInterface, LocalhomeInterfaces, Localinterface, and click OK button, all task structure is shown in the following figure:

(8) Right-click on the project, select the Run XDoclet in the pop-up menu to automatically generate a deployment descriptor and the corresponding file class file.

The execution console appears as follows:

At this point the project catalog structure is as follows:

Three test articles

We need to do some jobs before testing, first downloading JUnit and JuniteJB, we can unwind the JUniteJB.jar file with WinRAR, etc., and we found that the Meta-INF folder already included. The EJB deployment descriptor and JBoss container deployment descriptor, indicating that we can deploy them directly into JBoss.

However, before deployment, we need to join all subcles under junit.jar into junitejb.jar files, and specific operations can be done directly with compression software, or use JAR named. The internal file structure of the junitejb.jar after the addition is completed.

Second, copy the successful junitejb.jar file to the server / default / deploy directory under the JBoss directory.

Finally, we need to add junit.jar and junitejb.jar files into the ClassPath of the project. For details, please refer to the procedure after the completion of the project, Libraries as shown in the figure:

2. Write the test file: Click New button àjunitàtestcase

After selecting, click Next to fill in the pop-up window:

Here you need to note that TestCMP's parent class is not the original default junit.framework.testcase but is Net.SourceForge.junitejb.EJBTestCase (before you have junit.jar and junitejb.jar files have been added to Engineering ClassPath). After completing, click the finish button file, and the code for TestCMP can be found in the source code file of the document.

four. Deployment

1. Set up JBOSS server debugging: Click the down arrow next to the toolbar, select Debug in the pop-up menu ...

Select JBoss 3.0.x in the pop-up dialog, then click the bottom New button

Select the JBoss installation directory in JBoss 3.x Home Directory in the dialog that appears on the left:

After completing, click the Debug button to start the JBoss server. When the server is run, we can click the STOP button in the console to stop the running of the server.

Note that before starting the database server, make sure the MySQL server has started and contains a database named ResultManager, while there is an operational permissions of the database with the same username and password you set in front of the data source. 2. Pack the bean file as a JAR file: Right click on the SRC folder in the project, select the Export option in the pop-up menu, as shown in the figure:

Select JAR File in the pop-up dialog box, click Next, in the next pop-up dialog box, in the JAR file in the dialog box, fill in the path to store the JAR file, then click FINSH.

3. Deploy: Copy the exported JAR file to the Server / Default / Deploy directory under the JBoss directory (Make sure the JBoss server is running), at this time, check the console, there will be the following information, explain the deployment.

4. Test: First select the testcmp.java file in the project, then select the down arrow next to the toolbar, select Run Asàjunit Test to start the test in the right menu that pops up.

After the test is complete, check the test results, as shown in the following figure, the test is successful.

Five.

So we used Eclipse successfully developed CMP applications, but the steps were cumbersome to seriously affect development efficiency, and it was easy to generate errors, which is advantageous. Listed to deepen the understanding of EJB with programmers, understanding many fast development J2EE tools hidden; the prickship is the serious impact of development efficiency, and with the expansion of the project, the maintenance cost is constantly rise. Article in the source code download

About the Author:

Cai Yonghang, the third-grade student of Industrial and Commercial Management of Nanjing University of Technology, member of Mars Studio (http://online.njut.edu.cn/mars), member of the Chinese Union (http://www.robochina.org) Amateur time Cool Java programming. Contact: IamcyhaSP@sohu.com.

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

New Post(0)