openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kevin Sutter" <kwsut...@gmail.com>
Subject Re: JPA in JEE application client
Date Tue, 27 May 2008 12:23:11 GMT
Phani,
My first guess is that you are not completing these "persistable" operations
via a transactional commit.  Since you are not using EJBs, the demarcation
for a transaction is left up to the application.  You could use the
EntityTransaction interface to begin and commit/rollback your transactions.
You can either start the transaction before performing your "persistable"
operations, or you will have to call joinTransaction() if the transaction is
started after you perform your "persistable" operations.

Kevin

On Mon, May 26, 2008 at 6:57 AM, Phani Madgula <
phanibalaji.madgula@gmail.com> wrote:

> Hi,
>
> I have simple JEE application client that uses JPA to perform DB
> operations on a database running in the embedded Derby. I tried to attached
> the app to the mail but failed.
> The JEE client does not look up anyEJBs. It has Account.java entity and
> AccountClient.java that performs
> DB operations on the Entity.
>
> The following steps explain how to deploy and run the APP.
> 1. Create AccountDB database using DBManager portlet on the admin console
>
> 2. Create Account table in the AccountDB as follows
>  *create table ACCOUNT (ACCOUNTNO integer, NAME varchar (50),
> ADDRESS varchar (225), BRANCHCODE integer, BALANCE decimal (15,2));*
>
> 3. Deploy the app.jar
>
> 4. run the client using following options
>
> LISTING ACCOUNTS:
>
> j*ava -Djava.endorsed.dirs="C:**\Geronimo-2.1\lib\endorsed" -jar
> C:\Geronimo-2.1\bin\client.jar
> AccountJPA/AccountJPA-app**-client/3.0/jar list
> *
> CREATING AN ACCOUNT:
>
> * java -Djava.endorsed.dirs="C:**\Geronimo-2.1\lib\endorsed" -jar
> C:\Geronimo-2.1\bin\client.jar
> AccountJPA/AccountJPA-app**-client/3.0/jar create 2222 Joe NC 10 4000
> *
> UPDATING AN ACCOUNT:
>
> * java -Djava.endorsed.dirs="C:**\Geronimo-2.1\lib\endorsed" -jar
> C:\Geronimo-2.1\bin\client.jar
> AccountJPA/AccountJPA-app**-client/3.0/jar update 2222 8000*
>
> DELETING AN ACCOUNT
>
> j*ava -Djava.endorsed.dirs="C:**\Geronimo-2.1\lib\endorsed" -jar
> C:\Geronimo-2.1\bin\client.jar
> AccountJPA/AccountJPA-app**-client/3.0/jar delete 2222
> *
> The AccountClient.java has all the info.
>
> I am able to successfully deploy the APP and perform "list" operation.
> The list operation lists all the accounts currently in the database.
>
> Where as if I tried to perform "create", or "update" or "delete"
> operations, the JPA is not inserting or updating or deleting
> corresponding rows in the table. Neither it's throwing any error on
> the console.
>
> What could be the error?? Thanks in advance for your help.
>
> persistence.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" 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="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>
>
>      <!--
>      <jta-data-source>PhoneBookPool</jta-data-source>
>      <non-jta-data-source>PhoneBookPool</non-jta-data-source>
>      -->
> </persistence>
> _______________________________________________________________________
>
>
> ACCOUNT.java
> ________________________________________________________________________
>
> package sample.jpa.appclient;
>
> import java.io.Serializable;
>
> import javax.persistence.Entity;
> import javax.persistence.Id <http://javax.persistence.id/>;
> import javax.persistence.Table;
>
> @Entity
> @Table(name="Account1")
> public class Account implements Serializable{
>
>      @Id
>      private int accountNo;
>      private String name;
>      private String address;
>      private int branchCode;
>      private 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;
>      }
>
> }
> _______________________________________________________________________
>
> AccountClient.java
> ________________________________________________________________________
> package sample.jpa.appclient;
>
> import java.util.ArrayList;
> import java.util.List;
>
> import javax.persistence.EntityManager;
> import javax.persistence.EntityManagerFactory;
> import javax.persistence.Persistence;
> import javax.persistence.PersistenceContext;
> import javax.persistence.Query;
>
>
>
> public class AccountClient {
>
>      private EntityManager em;
>
>      public AccountClient()
>      {
>              EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("JPA-App-Client");
>              if(emf == null) System.out.println("emf is null!!!");
>              em = emf.createEntityManager();
>              if(em == null) System.out.println("em is null!!!");
>      }
>      public static void main(String[] args) {
>              AccountClient client = new AccountClient();
>
>              String opt = args[0];
>              if(opt.equals("create")){
>                      if (args.length != 6){
>                              System.out.println("Enter values for
> accountNo, name, address,
> branchCode and balance;");
>                              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 (opt.equals("list")){
>                      List accList = client.listAccounts();
>                  for(int i = 0; i < accList.size(); i++){
>
> System.out.println("____________________________________________");
>                      Account acc = (Account)accList.get(i);
>                      System.out.println(acc.getAccountNo());
>                      System.out.println(acc.getName());
>                      System.out.println(acc.getAddress());
>                      System.out.println(acc.getBranchCode());
>                      System.out.println(acc.getBalance());
>
> System.out.println("____________________________________________");
>                      System.out.println("");
>                  }
>              }else if (opt.equals("update")){
>                      if (args.length != 3){
>                              System.out.println("Enter values for
> accountNo and new balace value ;");
>                              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("Enter values for
> accountNo for delete");
>                              System.exit(0);
>                      }
>                      else {
>                              int accNo = Integer.parseInt(args[1]);
>                              client.deleteAccount(accNo);
>                      }
>              }else
>              {
>                      System.out.println("Unknown option 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+")");
>              em.persist(acc);
>              System.out.println("Persisted successfully account
> entity (accNo =
> "+accNo+")");
>              return acc;
>
>      }
>
>      public List listAccounts(){
>              if(em == null) System.out.println("em is null!!!");
>              Query q = em.createQuery("SELECT a FROM Account a");
>              List 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.setBalance(newBalance);
>              em.merge(acc);
>               return acc;
>
>      }
>
>      public void deleteAccount(int accNo){
>              Account acc = em.find(Account.class, accNo);
>              if(acc == null) throw new IllegalArgumentException("Account
> not
> found : Account Number ("+accNo+")");
>              em.remove(acc);
>      }
>
>
> }
>
> ____________________________________________________________________________
>
> geronimo-application-client.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>
> __________________________________________________________________________
>
> Thanks
> Phani
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message