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 Thu, 08 May 2008 21:19:35 GMT
Hello Phani,

It looks like you are correct about not needing to specify container 
managed persistence.  It is the default.

After lots of playing with your Account and BankBean classes, it looks 
like OpenJPA is fine with having your annotations (in Account.java) on 
the fields (field access).  But it doesn't seem to be detecting when the 
fields are directly changed.

So the quick way to get things working is to set up getters and setters 
on your entity class and use those for altering your entities (basically 
switch to using property access).

I have never used field access in OpenJPA, so I don't know if you are 
supposed to need to use setters.

This is probably something that should be brought up on the OpenJPA lists.

Do you want to, or should I?

Jay

Phani Madgula 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