uCosminexus Application Server, System Setup and Operation Guide

[Contents][Glossary][Index][Back][Next]

Appendix M.5 JPA sample programs

The sample programs using JPA are stored in the following directory:

Cosminexus-installation-directory\CC\examples\jpa\employeeinfo

Following is the overview and execution procedure of the sample:

Reference note
JPA sample program uses Cosminexus JPA provider with annotations. For mapping by using the O/R mapping file, see (3) Method of using the O/R mapping file.
Organization of this subsection
(1) Overview of JPA sample program
(2) Execution procedure of JPA sample program
(3) Method of using the O/R mapping file

(1) Overview of JPA sample program

Following figure shows the configuration of JPA sample program. In this sample program, the program acquires the employee information and updates the data. Employee information cannot be newly registered or deleted from the database table. The program detects the optimistic lock, if databases change during the processing of the find method to flush the method that is executed on clicking the Update button

Figure M-32 Configuration of JPA sample program

[Figure]

The program accesses the servlet by using the Web browser and displays the execution result on the browser. This sample program calls Stateless Session Bean from the servlet and operates the database by using JPA of Bean.

The following table describes the configuration contents of JPA sample program:

Table M-3 Configuration contents of JPA sample program

Item Contents
Type of Enterprise Bean Stateless Session Bean
Transaction CMT
Entity Manager Container management
Type of transaction JTA
Persistent context Scope of transaction

The following figure shows the configuration of sample program entity.

Figure M-33 Configuration of entities

[Figure]

As shown in the figure, the sample program uses unilateral ManyToOne in the relation between entities. Only the Employee entity class is concretized and the Department entity is used as the internal value of Employee entity. The optimistic lock functionality is implemented in both the entities.

Following figure shows the execution example of the sample program

Figure M-34 Execution example of JPA sample program

[Figure]

The following are the operations in this window:

  1. Enter the employee ID in ID and click the Find button.
    Employee information is displayed.
    You can specify 1~5 of the data registered in the ID column of JPA_SAMPLE_EMPLOYEE table, in Employee ID. For the data registered in the JPA_SAMPLE_EMPLOYEE table, see (2)(h) Executing SQL. If the entered value does not match the corresponding Employee ID, a message is displayed in the dialog box.
  2. Change the employee salary displayed in Salary and click the Update button.
    The changed employee salary is registered in the database. If the changed value is invalid or if there is no value in Salary, the following process is executed:
    • If the changed value is invalid
      The message is displayed on the window.
    • If there is no value in Salary
      Process is executed considering 0 as the entered value.

No process is executed even if you click the Find or Update button without entering a value in [ID].

(2) Execution procedure of JPA sample program

The following procedure describes the method of executing the sample program:

(a) Setting the J2EE server

See Appendix M.1(1) Setting the J2EE server.

(b) Customizing the J2EE server

See Appendix M.1(2) Customizing the J2EE server.

(c) Starting the J2EE server

See Appendix M.1(3) Starting the J2EE server.

(d) Setting the resource adapter

The resource adapter must be set to connect this sample program to the database. Set a value other than "NoTransaction" in the transaction support level in the Connector property file to enable the usage of JTA transaction. For the method of setting the resource adapter, see Appendix M.1(4) Setting the resource adapter.

(e) Setting the persistence.xml

Change the following tags of persistence.xml provided in the sample:

(f) Compiling the application

Compile the sample programs by using a batch file (compile.bat) provided in the sample. This batch file creates EJB-JAR file (employeeinfo.jar), WAR file (employeeinfo.war), and EAR file (employeeinfo.ear).

(g) Deploying the application

Use the batch file (deployApp.bat) provided in the sample to deploy the application. See Appendix M.1(5) Importing, setting and starting the application.

(h) Executing SQL

Execute the SQL in the SQL file (createTable_Oracle.sql, createTable_HiRDB.sql) provided in the sample. Execute SQL to remove, and add the JPA_SAMPLE_EMPLOYEE table and JPA_SAMPLE_DEPARTMENT table. The configuration and registration data of each table is as follows:

(i) Executing the application

Start the browser and specify the following string in the URL:

http://Machine-name: Port-number/ContextRoot/EmployeeInfo.html

If the in-process HTTP server is available, the following URL is specified by default:

http://localhost/employee_information/EmployeeInfo.html

(j) Undeploying the application

See Appendix M.1(6) Stopping and deleting the application

(k) Stopping the J2EE server

See Appendix M.1(7) Stopping the J2EE server

(3) Method of using the O/R mapping file

This sample program provides also the O/R mapping file (orm.xml). Since this program uses annotation, it does not use the O/R mapping file. Even if the sample program is compiled by using the batch file provided in the sample; the O/R mapping file is not stored in JAR file.

Note the following when using the O/R mapping file: