...
- Setting up Eclipse for Application development
- Developing Entities and Client
- Preparing Deployment Descriptors and Deployment Plans
- Deploying the application client
- Running the application client
To download the complete application, click on link
Setting Eclipse for Application development
1. Start the Eclipse IDE and create ApplicationClientJPA
java project.
Right click on the Project Explorer Window and click on New > Java Project.
...
That's all! the setup required for developing the application client is over.
Developing Entities and the Client
1.Right click on the project and create a Account
java class as follows.
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
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 + ")"); } acc.EntityTransaction et = em.getTransaction(); et.begin(); acc.setBalance(newBalance); emet.mergecommit(acc); return acc; } public void deleteAccount(int accNo) { EntityTransaction et = em.remove(em.getReference(Account.class, accNo)); } } getTransaction(); et.begin(); em.remove(em.getReference(Account.class, accNo)); et.commit(); } } |
Note |
---|
The |
Preparing Deployment Descriptors and Deployment Plans
1. Create persistence.xml
file in the META-INF
folder as follows.
Right click on the META-INF
folder and click on New => File.
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<?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> |
Deploying the
...
application client
1. Export the Java Project to a jar file Start the geronimo server and open the admin console http://localhost:8080/console
.
Click on Embedded DB => DB Manager on the Console Navigation portlet. This will open up DB Viewer and Run SQL portlets on the right hand side as follows.
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
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); |
32. Export the Java project by name ApplicationClientJPA.jar
as follows.
Right click on the java project and export it as jar file.
...
In the next screen click Next. In the Jar Manifest Specification wizard, select sample.jpa.appclient.AccountClient
using Browse button. This is the class that has public static void main(String[] args)
method which is the application entry point.
Click Finish.
23. Deploy the application client as follows.
Change the directory to <geronimo_home>\bin
.
Submit the below command
No Format bgColor #000000 borderStyle solid
<geronimo_home>/bin>deploy.bat --user system --password manager deploy <location_of_the_exported_jar_file>ApplicationClientJPA.jar>/ApplicationClientJPA.jar
Running the application client
1. Run the application client as follows.
The application performs the following operations. Use the script client
(.bat or .sh) to run 1. Run the application client as follows.
The application performs the following operations.
- List : This option lists the accounts curently in the database. The command to list is as follows.
No Format bgColor #000000 borderStyle solid <geronimo_home>/bin>java -Djava.endorsed.dirs="<geronimo_home>/lib/endorsed" -jar <geronimo_home>/bin/client.jar bin>client AccountJPA/AccountJPA-app-client/3.0/jar list
- Create : This option creates an account in the database. The command to create is as follows.
No Format bgColor #000000 borderStyle solid <geronimo_home>/bin>java -Djava.endorsed.dirs="<geronimo_home>/lib/endorsed" -jar <geronimo_home>/bin/client.jar bin>client.bat/sh AccountJPA/AccountJPA-app-client/3.0/jar create 2222 Joe NewYork BranchX 20000.0
- Update : This option updates an account with a new balance in the database.
The command to update is as follows.
No Format bgColor #000000 borderStyle solid <geronimo_home>/bin>java -Djava.endorsed.dirs="<geronimo_home>/lib/endorsed" -jar <geronimo_home>/bin/client.jar bin>client.bat/sh AccountJPA/AccountJPA-app-client/3.0/jar update 2222 30000.00
- Delete : This option deletes an account in the database. The command to delete is as follows.
No Format bgColor #000000 borderStyle solid <geronimo_home>/bin>java -Djava.endorsed.dirs="<geronimo_home>/lib/endorsed" -jar <geronimo_home>/bin/client.jar bin>client.bat/sh AccountJPA/AccountJPA-app-client/3.0/jar delete 2222.