geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Phani Madgula" <phanibalaji.madg...@gmail.com>
Subject Re: JPA Problem
Date Thu, 08 May 2008 11:15:14 GMT
Hi,

In the BeankBean.java there is another method "update()" as follows.

___________________________________________________________
   public void update(Account a) {
        manager.merge(a);
    }
_____________________________________________________________

I called this method in the servlet as follows.

______________________________________________________________

 account.balance=1000;

                   bank.update(account);

                    out.println("<h2>Printing the account details after
merging the amount</h2>");
                    account =
bank.findAccount(Integer.parseInt(request.getParameter("accNo")));

                       out.println("account Number =
"+account.accountNumber);
                       out.println("ownerName = "+account.ownerName);
                       out.println("balance = "+account.balance);
                       out.println("version = "+account.version);*/
_________________________________________________________________

In the above case, the database table field account.balance is updated with
1000. But somehow I am not able to make "deposit()" function work.

I tried even the following code as well for "deposit()" method without any
success.

____________________________________________________________________
   public void deposit(int accountNumber, int amount) {
            Account account = manager.find(Account.class, accountNumber);
            account.balance += amount;
            System.out.println("New balance="+account.balance);
            manager.merge(account);
        }

____________________________________________________________________


Can somebody throw some light on this issue??

Thanks
Phani

On Thu, May 8, 2008 at 10:16 AM, Phani Madgula <
phanibalaji.madgula@gmail.com> wrote:

> After the step (4), please insert a row in the account table as follows.
>
> "insert into account values (1, 'Owner Name1', 500, 1);"
>
> Thanks
> Phani B madgula
>
>
>
> On Thu, May 8, 2008 at 10:14 AM, Phani Madgula <
> phanibalaji.madgula@gmail.com> wrote:
>
>> Hi Jay,
>> I have not explicitly declared transaction configuration. So, the server
>> should run the the method with "TxRequired". The same info has been logged
>> to geronimo.log.
>>
>> I am attaching the apps 1) EJB app 2) WEB app to test ejb app. You may
>> have a look at the apps. The following are the steps to recreate the
>> problem.
>>
>> 1. Create BankDB database in the embedded derby db server using DB manager
>> portlet.
>> 2. Create Account table using "create table account (accountnumber
>> integer, ownername varchar(50), balance integer, version integer)"
>> 3. Deploy the EJB app
>> 4. Deploy the WEB app
>> 5. Hit the URL in browser window -->
>> http://localhost:8080/AccountCreateWEB/Test?accNo=1
>>
>> We could see that servlet is calling deposit(accountnumber ,100) method
>> but the value is not being updated in the database.
>>
>> Thanks
>> Phani
>>
>>
>> On Wed, May 7, 2008 at 2:56 PM, Jay D. McHugh <jaydmchugh@gmail.com>
>> wrote:
>>
>>> 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