...
2. Copy the below contents to the Account.java
.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | Account.javaJAVA |
---|
|
package sample.jpa.appclient;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Account1")
public class Account implements Serializable{
private static final long serialVersionUID = 1L;
@Id
public int accountNo;
public String name;
public String address;
public int branchCode;
public double balance;
public Account(){
this.accountNo = 0;
this.name = "DUMMY";
this.address = "DUMMY";
this.branchCode = 0;
}
public int getAccountNo() {
return accountNo;
}
public void setAccountNo(int accountNo) {
this.accountNo = accountNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getBranchCode() {
return branchCode;
}
public void setBranchCode(int branchCode) {
this.branchCode = branchCode;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
|
...
4.Copy the below contents to the AccountClient.java
.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | AccountClient.javaJAVA |
---|
|
package sample.jpa.appclient;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityTransaction;
public class AccountClient {
private EntityManager em;
public AccountClient() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA-App-Client");
if (emf == null) {
throw new IllegalStateException("EntityManagerFactory is unavailable");
}
em = emf.createEntityManager();
if (em == null) {
throw new IllegalStateException("EntityManager is unavailable");
}
}
public static void main(String[] args) {
AccountClient client = new AccountClient();
String opt = args[0];
if ("create".equals(opt)) {
if (args.length != 6) {
System.err.println("No values for accountNo, name, address, branchCode and balance. Exiting.");
System.exit(0);
} else {
int accNo = Integer.parseInt(args[1]);
String name = args[2];
String address = args[3];
int branchCode = Integer.parseInt(args[4]);
double balance = Double.parseDouble(args[5]);
client.createAccount(accNo, name, address, branchCode, balance);
}
} else if ("list".equals(opt)) {
List<Account> accounts = client.listAccounts();
for (Account account: accounts) {
System.out.println("_________________________________");
System.out.println(account.getAccountNo());
System.out.println(account.getName());
System.out.println(account.getAddress());
System.out.println(account.getBranchCode());
System.out.println(account.getBalance());
System.out.println("____________________________________________");
System.out.println("");
}
} else if ("update".equals(opt)) {
if (args.length != 3) {
System.out.println("No values for accountNo and new balace value. Exiting.");
System.exit(0);
} else {
int accNo = Integer.parseInt(args[1]);
double newbalance = Double.parseDouble(args[2]);
client.updateAccountBalance(accNo, newbalance);
}
} else if (opt.equals("delete")) {
if (args.length != 2) {
System.out.println("No values for accountNo for delete. Exiting.");
System.exit(0);
} else {
int accNo = Integer.parseInt(args[1]);
client.deleteAccount(accNo);
}
}
else {
System.err.println("Unknown option (" + opt + ") selected");
}
}
public Account createAccount(int accNo, String name, String address, int branchCode, double balance) {
Account acc1 = em.find(Account.class, accNo);
if (acc1 != null) {
throw new IllegalArgumentException("Account already exists - account Number (" + accNo + ")");
}
Account acc = new Account();
acc.setAccountNo(accNo);
acc.setAddress(address);
acc.setBalance(balance);
acc.setBranchCode(branchCode);
acc.setName(name);
System.out.println("Persisting account entity (accNo = " + accNo + ")");
EntityTransaction et = em.getTransaction();
et.begin();
em.persist(acc);
et.commit();
System.out.println("Persisted successfully account entity (accNo = " + accNo + ")");
return acc;
}
@SuppressWarnings("unchecked")
public List<Account> listAccounts() {
Query q = em.createQuery("SELECT a FROM Account a");
List<Account> currList = q.getResultList();
return currList;
}
public Account updateAccountBalance(int accNo, double newBalance) {
Account acc = em.find(Account.class, accNo);
if (acc == null) {
throw new IllegalArgumentException("Account not found : Account Number (" + accNo + ")");
}
EntityTransaction et = em.getTransaction();
et.begin();
acc.setBalance(newBalance);
et.commit();
return acc;
}
public void deleteAccount(int accNo) {
EntityTransaction et = em.getTransaction();
et.begin();
em.remove(em.getReference(Account.class, accNo));
et.commit();
}
}
|
...
Copy the below contents to the persistence.xml
.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | persistence.xml | XML |
---|
|
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0" >
<persistence-unit name="JPA-App-Client">
<description>JPA Application Client</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>sample.jpa.appclient.Account</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:derby://localhost/AccountDB" />
<property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="ConnectionUserName" value="app" />
<property name="openjpa.jdbc.SynchronizeMappings" value="false" />
</properties>
</persistence-unit>
</persistence>
|
2. Similarly, create application-client.xml
file in the META-INF
folder as given the previous step and copy the below contents into the file.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | application-client.xmlXML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<application-client xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application-client_5.xsd"
version="5">
</application-client>
|
...
3. Similarly, create geronimo-application-client.xml
file under META-INF
file and copy the below contents.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | geronimo-application-client.xml | XML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<application-client xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2"
xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2"
xmlns:security="http://geronimo.apache.org/xml/ns/security-2.0"
xmlns:connector="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2">
<sys:client-environment>
<sys:moduleId>
<sys:groupId>AccountJPA</sys:groupId>
<sys:artifactId>AccountJPA-app-client</sys:artifactId>
<sys:version>3.0</sys:version>
<sys:type>jar</sys:type>
</sys:moduleId>
<sys:dependencies>
<sys:dependency>
<sys:groupId>org.apache.geronimo.configs</sys:groupId>
<sys:artifactId>transaction</sys:artifactId>
<sys:version>2.1</sys:version>
<sys:type>car</sys:type>
</sys:dependency>
</sys:dependencies>
</sys:client-environment>
<sys:server-environment>
<sys:moduleId>
<sys:groupId>AccountJPA</sys:groupId>
<sys:artifactId>AccountJPA-app-client-server</sys:artifactId>
<sys:version>3.0</sys:version>
<sys:type>jar</sys:type>
</sys:moduleId>
</sys:server-environment>
</application-client>
|
...
In the textarea SQL Command/s: which is on the Run SQL portlet, enter the below SQL command, and select AccountDB in the Use DB combo box, and click on Run SQL button. This creates the Account1 table in the AccountDB database.
Code Block |
---|
| SQL |
---|
| SQL |
---|
borderStyle | solid |
---|
title | Account1 tableSQL |
---|
|
create table ACCOUNT1 (ACCOUNTNO integer, NAME varchar(50), ADDRESS varchar(225), BRANCHCODE integer, BALANCE decimal(15,2));
|
Insert some sample rows in the table using the below SQL statements by following the same outlined procedure above.
Code Block |
---|
| SQL |
---|
| SQL |
---|
borderStyle | solid |
---|
title | Account1 tableSQL |
---|
|
insert into Account1 values (1111, 'Joe', 'NewYork', 10, 30000.0);
insert into Account1 values (2222, 'John', 'NewJersy', 11, 31000.0);
insert into Account1 values (3333, 'Jane', 'Raleigh', 13, 32000.0);
|
...