Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

8. Copy the following contents into Account.java.

Code Block
JAVA
JAVA
borderStylesolid
titleAccount.javaJAVA
package sample.jpa;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PostLoad;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;


@Entity
@Table(name = "ACCOUNTBME")
public class Account implements Serializable {

 @Id
 public int accountNumber;
 public int personId;
 public double balance;

 public Account() {
 }

 public int getAccountNumber() {
  return accountNumber;
 }

 public void setAccountNumber(int accountNumber) {
  this.accountNumber = accountNumber;
 }

 public int getPersonId() {
  return personId;
 }

 public void setPersonId(int personId) {
  this.personId = personId;
 }

 public double getBalance() {
  return balance;
 }

 public void setBalance(double balance) {
  this.balance = balance;
 }
}

9. Similarly, as given in the previous step, create Person.java and add the following contents to it.

Code Block
JAVA
JAVA
borderStylesolid
titlePerson.javaJAVA
package sample.jpa;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "PERSONBME")
public class Person implements Serializable {

 @Id
 public int personId;
 public String personName;
 public String address;

 public Person() { }

 public int getPersonId() {
  return personId;
 }

 public void setPersonId(int personId) {
  this.personId = personId;
 }

 public String getPersonName() {
  return personName;
 }

 public void setPersonName(String personName) {
  this.personName = personName;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  System.out.println("Address="+address);
  this.address = address;
 }

}

9. Similarly, create AccountInterface.java and copy the following contents.

Code Block
JAVA
JAVA
borderStylesolid
titleAccountInterface.javaJAVA
package sample.jpa;

import java.util.Collection;

public interface AccountInterface {
 public void initialize(int accountNumber);
 public String getPersonName();
 public void setPersonName(String personName);
 public String getAddress();
 public void setAddress(String address);
 public double getBalance();
 public void setBalance(double balance);
 public void updateAllValues(String address, 
                             String personName, 
                             double balance);
 public void withdraw(double amount);
 public void deposit(double amount);
}

10. Similarly, create AccountBean.java.java and copy the following contents.

Code Block
JAVA
JAVA
borderStylesolid
titleAccountBean.javaJAVA
package sample.jpa;

import javax.annotation.PostConstruct;
import javax.ejb.Local;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;


@Stateful
@TransactionManagement(TransactionManagementType.CONTAINER)
@Local(AccountInterface.class)
public class AccountBean implements AccountInterface{

 @PersistenceUnit(unitName="AccountUnit")
 private EntityManagerFactory emf;

 private EntityManager em;

 Account account;
 Person person;

 @PostConstruct
 public void init(){
  em = emf.createEntityManager();
 }

 public void initialize(int accountNumber){

  account = em.find(Account.class, accountNumber);

  if(account == null)throw new IllegalStateException
   ("Account number ("+accountNumber+") not found");

  person = em.find(Person.class, account.getPersonId());

  if(person == null)throw new 
   IllegalStateException("Person Id("+account.getPersonId()+") not found. 
   There is a descripancy in the database for 
   account number ("+accountNumber+")");
 }

 public String getPersonName(){
  return person.getPersonName();
 }

 public void setPersonName(String personName){
  person.setPersonName(personName);
 }

 public String getAddress(){
  return person.getAddress();
 }

 public void setAddress(String address){
  person.setAddress(address);
 }

 public void updateAllValues(String address, 
                             String personName, 
                             double balance){
  em.joinTransaction();
  setAddress(address);
  setPersonName(personName);
  setBalance(balance);
 }

 public double getBalance(){
  return account.getBalance();
 }

 public void setBalance(double balance){
  account.setBalance(balance);
 }

 public void withdraw(double amount){

  if(amount > getBalance())throw new IllegalStateException
    ("The amount("+amount+") to be withdrawn is more than   
    the available balance("+getBalance()+")");

  em.joinTransaction();
  setBalance(getBalance() - amount);

 }

 public void deposit(double amount){
  em.joinTransaction();
  setBalance(getBalance() + amount);
 }

 @Remove
 public void destroy(){
  em.close();
 }
}

11. As outlined above, right click on the META_INF directory of BeanManagedJPA-EJB project and
create persistence.xml. Copy the following contents into persistence.xml.

Code Block
XML
XML
borderStylesolid
titlepersistence.xmlXML
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence   
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

 <persistence-unit name="AccountUnit" transaction-type="JTA">
  <description>ContainerManagedJPA</description>
  <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
  <jta-data-source>AccountDS</jta-data-source>
  <class>sample.jpa.Account</class>
  <class>sample.jpa.Person</class>
 </persistence-unit>
</persistence>

12. Since we are going to use EJB annotations, the META-INF/ejb-jar.xml will not have any declarations. The contents of the META-INF/openejb-jar.xml file should be as below. Otherwise, modify it accordingly.

Code Block
XML
XML
borderStylesolid
titleopenejb-jar.xmlXML
<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar xmlns="http://openejb.apache.org/xml/ns/openejb-jar-2.2" 
 xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2" 
 xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" 
 xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">

<sys:environment>
 <sys:moduleId>
  <sys:groupId>BeanManagedJPA</sys:groupId>
  <sys:artifactId>EJB</sys:artifactId>
  <sys:version>1.0</sys:version>
  <sys:type>car</sys:type>
 </sys:moduleId>

 <dependencies>
  <dependency>
   <groupId>console.dbpool</groupId>
   <artifactId>AccountDS</artifactId>
  </dependency>
 </dependencies>
</sys:environment>
<enterprise-beans/>
</openejb-jar>

...

5. Right click on the WebContent folder of the web project and navigate to New => HTML to create the index.html file as given in the screen shot. Click on the Next button and on the next screen click on the Finish button. The contents of the index.html is provided below the screen shot.

Code Block
ActionScript
ActionScript
borderStylesolid
titleindex.htmlActionScript
<!DOCTYPE html PUBLIC 
 "-//W3C//DTD HTML 4.01 Transitional//EN" 
 "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Bean Managed JPA</title>
 </head>

<body>

 <font size=4>
   <a href="/BeanManagedJPA-WEB/ViewAccount.html">
    View Account Details
   </a>
 </font><br/>

 <font size=4>
  <a href="/BeanManagedJPA-WEB/TransferAmount.jsp">
    Transfer Amount
  </a>
 </font>

</body>

</html>

6. Right click on the WebContent folder of the web project and navigate to New => HTML to create the ViewAccount.html file as given in the screen shot. Click on the Next button and on the next screen click on the Finish button. The content of the ViewAccount.html is provided below the screen shot.

Code Block
ActionScript
ActionScript
borderStylesolid
titleViewAccount.htmlActionScript
<!DOCTYPE html PUBLIC 
 "-//W3C//DTD HTML 4.01 Transitional//EN" 
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Bean Managed JPA</title>
</head>
<body>

<form name="input" 
 action="/BeanManagedJPA-WEB/RetrieveAccount.jsp"method="get">

<table border="0">
<tr>
<td align="right"><font color="black" size="4">Account Number</font></td>
<td align="left"><input type="text" name="accountNumber"></td>
</tr>
<tr>
<td align="right"><input type="submit" value="Submit"></td>
<td></td>
</tr>
</table>

</form>
</body>
</html>

9. Similarly, as illustrated in the previous steps, create RetrieveAccount.jsp. The contents of the of the jsp is as follows.

Code Block
ActionScript
ActionScript
borderStylesolid
titleRetrieveAccount.jspActionScript
<%@ page language="java" contentType="text/html; 
    charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="sample.jpa.AccountInterface"%> 
<%@ page import="javax.naming.Context"%>  
<%@ page import="javax.naming.InitialContext"%> 


<!DOCTYPE html PUBLIC 
 "-//W3C//DTD HTML 4.01 Transitional//EN" 
 "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" 
 content="text/html; 
 charset=ISO-8859-1">
<title>Retrieve and Update Account</title>
</head>
<body>

<%

String update = request.getParameter("Update");
String cancel = request.getParameter("Cancel");

System.out.println("update value="+update);
System.out.println("cancel value="+cancel);

String submitValue = null;
if(update != null) submitValue=update;
if(cancel != null) submitValue=cancel;

System.out.println("1 Submit value="+submitValue);

if(submitValue != null) {

 System.out.println("2 Submit value="+submitValue);
 if(submitValue.equals("Cancel")){
  System.out.println("3 Submit value="+submitValue);
  response.sendRedirect("/BeanManagedJPA-WEB/");
 }

else if(submitValue.equals("Update")){

 System.out.println("4 Submit value="+submitValue);
 System.out.println("Updating the Account Entity Started");

 Context ctx  = new InitialContext();

 System.out.println("Instantiating beans...");

 AccountInterface accountBean = (AccountInterface)
  ctx.lookup("java:comp/env/ejb/AccountInterface");

 int accountNumber = Integer.parseInt
  (request.getParameter("accountNumber"));

 accountBean.initialize(accountNumber);
 
 accountBean.updateAllValues(request.getParameter("address"), 
                             request.getParameter("personName"),
                             Double.parseDouble
                              (request.getParameter("balance")));

 System.out.println("Updating the Account Entity Ended");
 response.sendRedirect("/BeanManagedJPA-WEB/RetrieveAccount.jsp?
  accountNumber="+accountNumber);
}

} else{

 Context ctx  = new InitialContext();
 System.out.println("Instantiating beans...");
 AccountInterface accountBean = (AccountInterface)ctx.lookup("java:comp/env/ejb/AccountInterface");
 int accountNumber = Integer.parseInt(request.getParameter("accountNumber"));
 accountBean.initialize(accountNumber);
%>

<form name="input" action="/BeanManagedJPA-WEB/RetrieveAccount.jsp">
<table border="0"> 
<tr>
<td align="right"><font color="black" size="4"> Account Number :</font></td>
<td align="left"><font color="red" size="4"><%=accountNumber%></font>
<input type="hidden" name="accountNumber" value="<%=accountNumber%>">
</td>
</tr>
<tr>
<td align="right"><font color="black" size="4"> Person Name :
 </font></td>
<td align="left"><font color="red" size="4">
 <%=accountBean.getPersonName()%> 
 </font></td>
</tr>
<tr>
<td align="right"><font color="black" size="4"> Update Person Name :
 </font></td>
<td align="left"><input type="text" name="personName"></td>
</tr>
<tr>
<td align="right"><font color="black" size="4"> Address :
 </font></td>
<td align="left"><font color="red" size="4"><%=accountBean.getAddress()%> 
 </font></td>
</tr>
<tr>
<td align="right"><font color="black" size="4"> Update Address :
 </font></td>
<td align="left"><input type="text" name="address"></td>
</tr>
<tr>
<td align="right"><font color="black" size="4"> Balance :
 </font></td>
<td align="left"><font color="red" size="4"><%=accountBean.getBalance()%>
 </font></td>
</tr>
<tr>
<td align="right"><font color="black" size="4"> Update Balance :
 </font></td>
<td align="left"><input type="text" name="balance"></td>
</tr>
<tr>
<td align="right"><input type="submit" name="Update" 
 value="Update"></td>
<td align="left"><input type="submit" name="Cancel" 
 value="Cancel"></td>
</tr>
</table>
</form>
<%} %>
</body>
</html>

10. Similarly, as illustrated in the previous steps, create TransferAmount.jsp. The contents of the jsp is as follows.

Code Block
ActionScript
ActionScript
borderStylesolid
titleTransferAmount.jspActionScript
<%@ page language="java" contentType="text/html; 
 charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="sample.jpa.AccountInterface"%> 
<%@ page import="javax.naming.Context"%>  
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="javax.transaction.UserTransaction"%>
<!DOCTYPE html PUBLIC 
 "-//W3C//DTD HTML 4.01 Transitional//EN" 
 "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" 
 content="text/html; charset=ISO-8859-1">
<title>Transfer Amount</title>
</head>
<body>
<%
String submitValue = request.getParameter("Transfer");

if(submitValue!=null){

 int debitAccount = Integer.parseInt
 (request.getParameter("accountNumber1"));

 int creditAccount = Integer.parseInt
 (request.getParameter("accountNumber2"));

 double amount = Double.parseDouble
 (request.getParameter("amount"));

 Context ctx = new InitialContext();

 AccountInterface debitAccountBean = 
 (AccountInterface)ctx.lookup("java:comp/env/ejb/AccountInterface");

 debitAccountBean.initialize(debitAccount);

 AccountInterface creditAccountBean = 
 (AccountInterface)ctx.lookup("java:comp/env/ejb/AccountInterface");

 creditAccountBean.initialize(creditAccount);

 UserTransaction ut = 
 (UserTransaction)ctx.lookup("java:comp/UserTransaction");
 
 ut.begin();
 debitAccountBean.withdraw(amount);
 creditAccountBean.deposit(amount);
 ut.commit();

 response.sendRedirect("/BeanManagedJPA-WEB/");

}

else{

%>
<form name="input" action="/BeanManagedJPA-WEB/TransferAmount.jsp"
 method="get">
<table border="0">
<tr>
<td align="right"><font color="black" size="4">
  Debit Account Number :</font></td>
<td align="left"><input type="text" name="accountNumber1">
 </td>
</tr>
<tr>
<td align="right"><font color="black" size="4"> 
 Credit Account Number:</font></td>
<td align="left"><input type="text" 
 name="accountNumber2"></td>
</tr>
<tr>
<td align="right"><font color="black" size="4"> 
 Amount to be Transfered :</font></td>
<td align="left"><input type="text" name="amount"></td>
</tr>
<tr>
<td align="right"><input type="submit" name="Transfer" 
 value="Transfer"></td>
<td></td>
</tr>
</table>
</form>
<%} %>
</body>
</html>

...

4. The above step will create AccountDB database. On the same screen, enter the below SQL command on the SQL Command/s textarea and select AccountDB in the Use DB combo box and click on the Run SQL button. This will create ACCOUNTCME table in the AccountDB database.

Code Block
SQL
SQL
borderStylesolidSQL
create table ACCOUNTBME (ACCOUNTNUMBER integer, PERSONID integer, balance decimal(15,2));

...

5. Similarly, enter the below SQL command on the SQL Command/s textarea and select AccountDB in the Use DB combo box and click on the Run SQL button. This will create PERSONBME table in the AccountDB database.

Code Block
SQL
SQL
borderStylesolidSQL
create table PERSONBME (PERSONID integer, PERSONNAME varchar(255), ADDRESS varchar(255));

...

6. Insert the two rows using the below SQL command.

Code Block
SQL
SQL
borderStylesolidSQL
insert into PERSONBME values (1, 'Shane Gibson', 'XXXX');
insert into PERSONBME values (2, 'Rameez Raza', 'YYYY');
insert into ACCOUNTBME values (10,1,6500);
insert into PERSONBME values (11,2,9000);

...