geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jay D. McHugh" <jaydmch...@gmail.com>
Subject Re: JPA Problem
Date Wed, 07 May 2008 09:26:36 GMT
Phani,

Are you specifying that you want to use container managed persistence in 
your BankBean?

The way to do this with an annotation would be:
@TransactionManagement(TransactionManagementType.CONTAINER)

If you are specifying container managed persistence then you wouldn't 
need to worry about starting and committing your transaction - and the 
code for your 'deposit' method would work.  Actually, you don't even 
need the 'manager.merge(account)'.

But, if you are not specifying container managed persistence - then you 
need to get a UserTransaction and begin/commit it to have your account 
balance change in your database.

Hope that helps,

Jay

Phani Madgula wrote:
> Hi,
> 
> I got past the above error but experiencing some strange results as follows.
> 
> In the BankBean.java, I have the following method.
> 
> *********
>    public void deposit(int accountNumber, int amount) {
>             Account account = manager.find(Account.class, accountNumber);
>             account.balance += amount;
>             System.out.println("New balance="+account.balance);
>             System.out.println("Merging the account");
>             manager.merge(account);
>         }
> *********
> 
> I have the Account.java as follows.
> **********************************************************************
> package sample.jpa;
> 
> import java.io.Serializable;
> 
> import javax.persistence.Entity;
> import javax.persistence.Id;
> import javax.persistence.NamedQuery;
> import javax.persistence.PostLoad;
> import javax.persistence.PostUpdate;
> import javax.persistence.PrePersist;
> import javax.persistence.PreUpdate;
> import javax.persistence.Version;
> import javax.persistence.Table;
> 
> /**
>  * This demo entity represents a Bank Account.
>  * <p>
>  * The entity is not a remote object and can only be accessed locally by
>  * clients. However, it is made serializable so that instances can be passed
> by
>  * value to remote clients for local inspection.
>  * <p>
>  * Access to persistent state is by direct field access.
>  */
> @Entity
> @Table(name = "ACCOUNT")
> @NamedQuery(name="findThem", query="SELECT a FROM Account a")
> public class Account implements Serializable {
> 
>     /** The account number is the primary key for the persistent object */
>     @Id
>     public int accountNumber;
> 
>     public String ownerName;
> 
>     public int balance;
> 
>     @Version
>     public int version;
> 
>     /**
>      * Entity beans must have a public no-arg constructor
>      */
>     public Account() {
>         // our own primary key generation, workaround for the
>         // time being as persistence does not support
>         // auto-generation
>         accountNumber = (int) System.nanoTime();
>     }
> 
>     public String toString() {
>         return "Acc.# " + accountNumber + ", owner" + ownerName + ",
> balance: " + balance
>                 + " $";
>     }
> 
>     @PrePersist
>     void prepersist() {
>         System.out.println("pre persist!!");
>     }
> 
>     @PreUpdate
>     void preupdate() {
>         System.out.println("pre update!!");
>     }
> 
>     @PostUpdate
>     void postupdate() {
>         System.out.println("post update!!");
>     }
> 
>     @PostLoad
>     void postload() {
>         System.out.println("post load!!");
>     }
> 
> }
> **********************************************************************
> 
> In the servlet I have the following code
> 
> **********************************************************************
> 
>  bank = (Bank)ctx.lookup("BankBeanRemote");
>  account =
> bank.findAccount(Integer.parseInt(request.getParameter("accNo")));
> 
>                out.println("account Number =
> "+account.accountNumber+"<br/>");
>                out.println("ownerName = "+account.ownerName+"<br/>");
>                out.println("balance = "+account.balance+"<br/>");
>                out.println("version = "+account.version+"<br/>");
> 
> 
>                 accountNumber = account.accountNumber;
> 
>                 out.println("Depositing Rs.100"+"<br/>");
> 
>                 bank.deposit(accountNumber,100);
> 
> **********************************************************************
> 
> When I run the servlet, it is able to load the account values from the
> "account =
> bank.findAccount(Integer.parseInt(request.getParameter("accNo")));"
> statement. However, the "bank.deposit(accountNumber,100);" is not updating
> the "balance" field.
> 
> What is the problem?? I have the following persistence.xml file
> *****************************************************************************************************
> <?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="BankPU">
>         <description>Bank example</description>
> 
> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
>         <class>sample.jpa.Account</class>
>         <properties>
>             <property name="openjpa.ConnectionURL" value="jdbc:derby:BankDB"
> />
>             <property name="openjpa.ConnectionDriverName"
> value="org.apache.derby.jdbc.EmbeddedDriver" />
>             <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>
> 
> **********************************************************************************************************
> 
> On Wed, May 7, 2008 at 10:52 AM, Phani Madgula <
> phanibalaji.madgula@gmail.com> wrote:
> 
>> Hi,
>>
>> I have an EJB application using JPA and a web application looking up ejbs
>> in EJB application. I am able to deploy both the applications on AG2.1
>> server. But when I hit the servlet, I get the following error in the
>> console.
>> *****
>> Message: The bean encountered a non-application exception.; nested
>> exception is:
>>
>>         <openjpa-1.0.1-r420667:592145 fatal user error>
>> org.apache.openjpa.persi
>> stence.ArgumentException: Could not locate metadata for the class using
>> alias "A
>> ccount". This could mean that the OpenJPA enhancer or load-time weaver was
>> not r
>> un on the type whose alias is "Account". Registered alias mappings:
>> "{Account=nu
>> ll}"
>> *****
>>
>> The ejb app is attached to mail. Any suggestions on what is missing here??
>>
>> Thanks
>> Phani
>>
> 

Mime
View raw message