|
JPA App
Table of Contents
This sample takes you through the creation and deployment of a simple JPA application, Bank. In the sample application Bank, we will use an Enterprise Bean and simple JavaServer Pages to create a simple on-line bank application. In the sample, we can create an account and view the details of the account. A single Enterprise Bean, Account.java is used in this JPA application with a thin HTML client brought about by three JavaServer Pages. The walkthrough demonstrates the following:
In this sample, a Windows platform is assumed. To work on any other platform, follow the required file conventions on that platform. Creating a Directory Structure
- entity - META-INF - web/WEB-INF/classes - web/WEB-INF/lib Creating the Entity Class
/*
* Copyright (c) 2005 Pramati Technologies Private Ltd. All Rights Reserved.
*
* This software is the confidential and proprietary information of Pramati
* Technologies. You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the source code
* license agreement you entered into with Pramati Technologies.
*
*/
package entity;
import javax.persistence.*;
/**
* @author <a href="mailto:abc@pramati.com">ABC</a>
* @version $Revision: 1.12 $, $Date: 2007/06/29 05:16:21 $, $Author: root $
* @since Jan 30, 2007
*/
@Entity
@Table(name="ACCOUNT_TABLE")
public class Account
{
private int id;
private String Name;
private double balance;
public double getBalance()
{
return this.balance;
}
public void setBalance(double balance)
{
this.balance = balance;
}
@Id
@GeneratedValue
public int getId()
{
return this.id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return this.Name;
}
public void setName(String name)
{
this.Name = name;
}
}
Adding JSP Files to your Directories
<html> <h2>Welcome to Simple Bank Application </h2> <h3>Create Account</h3> <form name="CreateForm" method="post" action="createAccount.jsp"> <pre> Name : <input type="text" name="name" value=""/> Balance : <input type="text" name="bal" value=""/> </pre> <input type="submit" value="Create Account"/> </form> <br><br> <h3>View Account Details</h3> <form name="ViewForm" action="viewAccount.jsp"/> <pre> Enter your AccountNumber : <input type="text" name="actNumber" value=""> <br> </pre> <input type="submit" value = "View Account"/> </form> </html>
<%@ page import="javax.persistence.*"%>
<%@ page import="entity.Account" %>
<%
EntityManagerFactory emf = Persistence.createEntityManagerFactory("account-persistence-unit");
EntityManager em = emf.createEntityManager();
String name = request.getParameter("name");
double balance = Double.parseDouble(request.getParameter("bal"));
em.getTransaction().begin();
Account act = new entity.Account();
act.setName(name);
act.setBalance(balance);
em.persist(act);
em.getTransaction().commit();
pageContext.setAttribute("act", act);
%>
You have created the account Successfully!
Here are the Details: <br>
<b>Account Number : </b> <%= act.getId()%> <br>
<b>Name : </b> <%= act.getName() %> <br>
<b>Balance : </b> <%= act.getBalance() %> <br>
<a href ="index.jsp"> Home </a>
The file creates a new account for the user with the specified name and balance. The class also sets a unique account ID for each account created.
<%@ page import="javax.persistence.* , entity.*;" %>
<%
String actID = request.getParameter("actNumber");
EntityManagerFactory emf;
EntityManager em;
emf = Persistence.createEntityManagerFactory("account-persistence-unit");
em = emf.createEntityManager();
em.getTransaction().begin();
Account act = em.find(Account.class , Integer.parseInt(actID.trim()));
em.getTransaction().commit();
%>
Here are the Details: <br>
<b>Account Number : </b> <%= act.getId()%> <br>
<b>Name : </b> <%= act.getName() %> <br>
<b>Balance : </b> <%= act.getBalance() %> <br>
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<welcome-file-list>
<welcome-file>web/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Adding Resources to use the Derby Database ServerAs part of the JPA application development efforts, you will also need a database to persist the data. In our sample application here, we will use the database, Apache Derby.To use any other database, you will need to use different resources, and modify the scripts, entity files and tables for the application.
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="account-persistence-unit">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>entity.Account</class>
<properties>
<property name="toplink.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="toplink.jdbc.url" value="jdbc:derby:C:/Bank/bank_db;create=true"/>
<property name="toplink.jdbc.user" value=""/>
<property name="toplink.jdbc.password" value=""/>
<property name="toplink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
Creating Tables Using TopLink Essentials JARYou may choose to create database tables using some tool, create it programmatically using JPA, or use the JAR file, toplink-essentials.jar.In the following steps, we explain how to use the TopLink Essentials JAR to automatically create database tables.
The toplink-essentials.jar is basically the JPA provider for the EJB 3.0 Reference Implementation. In addition to being JPA compliant, it also provides additional extensions beyond what is defined in the JPA specification. For example, TopLink JPA Extensions for JDBC.
<property name="toplink.ddl-generation" value="create-tables"/>This allows the toplink-essentials.jar file to automatically create DDL for non-existent tables and leave the existing tables unchanged whenever you run the application. Adding Other JARs
Creating .bat Files
del entity\*.class javac entity\*.java mkdir web\WEB-INF\classes\entity mkdir web\WEB-INF\classes\META-INF copy entity\*.class web\WEB-INF\classes\entity copy META-INF\* web\WEB-INF\classes\META-INF
set classpath=C:\Bank\web\WEB-INF\lib\derby.jar;C:\Bank\web\WEB-INF\lib\jsp-api.jar;C:\Bank\web\WEB-INF\lib\toplink-essentials.jar;C:\Bank\web\WEB-INF\lib\servlet-api.jar;Here, we assume c:\Bank to be the application root directory. You may change this to whatever appropriate on your machine. Compiling the Java File
classpath.batin the command prompt.
build.batin the command prompt.
Deploying the Application
cd c:\Dekoh\server\bin
deploy c:\Bank\web Running the Application
Debugging your ApplicationThis section discusses how to debug the JSP application you just built using IDEs such as IntelliJIDEA and Eclipse SDK. In the following sections, we use the Debugger to:
Setting the Java Debug Option
-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7777to the command, java -server -cp %CLASSPATH% %COMPILER_OPTIONS% %DATEPARSING% %REDIRECTSTREAMS% com.pramati.web.lwt.LWWebServer -nodedir ../nodes/default -httpport 8181 %NOSHELL% Debug Using Eclipse
cd c:\Dekoh\server\bin
deploy c:\Bank\web
this.id = id;inside the setId method.
Debug Using IntelliJIDEA
cd c:\Dekoh\server\bin
deploy c:\Bank\web
this.id = id;inside the setId method.
Distributing your ApplicationNow that you have your application running, you may want to distribute it to end users who have installed Dekoh Desktop, and will like to work with your application.To make your application distributable to the end user:
Using the Compiled JSP Pages
Compiling the JSP Application Using ANT
LIB_DIR = C:\\Dekoh\\server\\lib
pramati.lwt.jar = ${LIB_DIR}/pramati/pramati_web_lwt.jar
app.root=C:/Bank
web.root = ${app.root}/web
class.dir = ${app.root}/web/WEB-INF/classes
ant.class.path = ${pramati.lwt.jar}
app.class.path = ${pramati.lwt.jar}:\
${web.root}/WEB-INF/lib/derby.jar:\
${web.root}/WEB-INF/lib/servlet-api.jar:\
${web.root}/WEB-INF/lib/jsp-api.jar:\
${web.root}/WEB-INF/lib/toplink-essentials.jar:\
${class.dir}:
app.path=C:/Bankto point to your Bank root directory.
<?xml version="1.0" encoding="UTF-8"?>
<project name="Bank" default="instrument">
<property file="das-jsp-compile.props"/>
<taskdef classname="com.pramati.tools.ant.AntTaskHandlerJSPCompiler" name="jspc">
<classpath id="jspc.classpath">
<pathelement path="${app.class.path}"/>
</classpath>
</taskdef>
<taskdef classname="com.pramati.tools.ant.AntTaskHandlerInstrument" name="instrument">
<classpath id="instrument.classpath">
<pathelement path="${app.class.path}"/>
</classpath>
</taskdef>
<target name="copy.persistence.xml">
<copy todir="${class.dir}">
<fileset dir="${app.root}">
<include name="/META-INF/persistence.xml"/>
</fileset>
</copy>
</target>
<target name ="javacompile" depends ="copy.persistence.xml">
<delete>
<fileset dir="${class.dir}" includes="${class.dir}/*.class"/>
</delete>
<javac srcdir="${app.root}" destdir="${class.dir}" includes="entity/*.java" classpath="${app.class.path}"></javac>
</target>
<target name="parse">
<jspc appRoot="${web.root}" validateXML="true" outputDir="${web.root}/WEB-INF/jsp_src">
<classpath>
<pathelement path="${app.class.path}"/>
</classpath>
</jspc>
</target>
<target name="webcompile" depends="javacompile , parse">
<mkdir dir="${web.root}/WEB-INF/classes"/>
<javac destdir="${web.root}/WEB-INF/classes" optimize="off" debug="on" failonerror="false" srcdir="${web.root}/WEB-INF/jsp_src">
<classpath>
<pathelement path="${app.class.path}"/>
</classpath>
</javac>
</target>
<target name="instrument" depends="webcompile">
<instrument srcDir="${web.root}/WEB-INF/jsp_src" classDir="${web.root}/WEB-INF/classes"/>
</target>
</project>
ant -f <full path to das-jsp-compile.xml>to compile the file, das-jsp-compile.xml Saving component.xml file
<?xml version="1.0" encoding="UTF-8" ?> - <component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dekoh.com/xmlns/component http://www.dekoh.com/schemas/component.xsd" xmlns="http://www.dekoh.com/xmlns/component" enabled="true"> <id>http://www.pramati.com/bank</id> <version>1.0</version> <type>application</type> <display-name>Bank</display-name> <description>A simple Bank application that allows you to create a new account and view the details of the account, given the account ID. </description> </component>The version manager in Dekoh compares your version against the currently available version of the application. This will either tell you that no upgrade is needed, or that an upgrade is recommended.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:component="http://www.dekoh.com/xmlns/component"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
targetNamespace="http://www.dekoh.com/xmlns/component">
<xs:element name="callback-classes">
<xs:complexType>
<xs:sequence>
<xs:element ref="component:callback-class" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="dependencies">
<xs:complexType>
<xs:sequence>
<xs:element ref="component:dependency" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="component">
<xs:complexType>
<xs:all>
<xs:element ref="component:id"/>
<xs:element ref="component:version"/>
<xs:element ref="component:type"/>
<xs:element ref="component:display-name" minOccurs="0"/>
<xs:element ref="component:description" minOccurs="0"/>
<xs:element ref="component:updateurl" minOccurs="0"/>
<xs:element ref="component:downloadurl" minOccurs="0"/>
<xs:element ref="component:dependencies" minOccurs="0"/>
<xs:element ref="component:callback-classes" minOccurs="0"/>
</xs:all>
<xs:attribute name="enabled" use="optional">
<xs:simpleType>
<xs:restriction base="xs:boolean"/>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="version" use="optional">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="dependency">
<xs:complexType>
<xs:all>
<xs:element ref="component:id"/>
<xs:element ref="component:maxversion"/>
<xs:element ref="component:minversion"/>
<xs:element ref="component:updateurl"/>
</xs:all>
<xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="description" type="xs:string"/>
<xs:element name="id" type="xs:string"/>
<xs:element name="maxversion" type="xs:string"/>
<xs:element name="minversion" type="xs:string"/>
<xs:element name="display-name" type="xs:string"/>
<xs:element name="type" type="xs:string"/>
<xs:element name="updateurl" type="xs:string"/>
<xs:element name="downloadurl" type="xs:string"/>
<xs:element name="callback-class" type="xs:string"/>
<xs:element name="version" type="xs:string"/>
</xs:schema>
Packaging the Application
jar -cfv Bank.war web/*.jsp web/WEB-INF/classes web/WEB-INF/lib
|