I posted the other day with a big problem - a test program I wrote to try
out optimistic locking does not generate SQL correctly. In the code, I
retrieve an object, update it, and try to store it again. OJB generates an
INSERT rather than an UPDATE, which of course causes an exception to be
thrown because of the primary key constraint. I have verified that the
record is actually being retrieved. I am using a version of OJB built from
CVS HEAD, retrieved about a week ago.

If anyone has seen anything like this, please let me know. I am completely
stumped.

I was asked to include more of the class which is causing the problem. I am
including it, as well as a dump from my logfile, and the relevant part of
repository_user.xml
Here is the class:
--------------------------------------------------------------------public
class AccountUpdater extends Thread
{
    private PersistenceBroker broker = null;
   
    public AccountUpdater()
    {
        super();
    }

    public void run()
    {
       try
        {
           broker = 
                 PersistenceBrokerFactory.createPersistenceBroker(new
PBKey("ActiveBill"));
    
           broker.beginTransaction();
       
           AccountInterface acc = retrieveAccountByNumber("1234");
           System.out.println("retrieved " + acc.getAccountNumber());
        
           acc.setTelephoneNumber("555-5555");
           System.out.println("will store updated account");
           broker.store(acc);
           System.out.println("will commit");
           broker.commitTransaction();
        }
       catch (Exception e)
            {
              broker.abortTransaction();
              System.out.print(e.getMessage());
              e.printStackTrace();
             }
       finally {
          broker.close();
       }        
    }  
    
    AccountInterface retrieveAccountByNumber(String acctNumber)
    {
        System.out.println(
            "in retrieveAccountByNumber, looking for " + acctNumber);
        Account account = null;

        Criteria crit = new Criteria();
        crit.addEqualTo("accountNumber", acctNumber);
      
        Query query = new QueryByCriteria(Account.class, crit, true);
        try
        {
            account = (Account) broker.getObjectByQuery(query);
           
        }
        catch (Exception e)
        {
            System.out.print(e.getMessage());
            e.printStackTrace();
        }
        return account;
    }
}

--------------------------------------------------------------------
The log file looks likes this :
12999 INFO  [Thread-11] metadata.RepositoryPersistor - Read class
descriptors took 12879 ms
12999 INFO  [Thread-11] metadata.RepositoryPersistor - OJB Descriptor
Repository:
file:C:/bea/user_projects/bonnie/applications/ActiveBill/WEB-INF/classes/rep
ository.xml
13109 INFO  [Thread-11] metadata.RepositoryPersistor - Read connection
repository took 110 ms
13379 INFO  [Thread-11] core.PersistenceBrokerFactoryDefaultImpl - Set
default PBKey: org.apache.ojb.broker.PBKey: jcdAlias=ActiveBill, user=admin,
password=*****
13459 INFO  [Thread-11] core.PersistenceBrokerFactoryDefaultImpl - Create
new PB instance for PBKey org.apache.ojb.broker.PBKey: jcdAlias=ActiveBill,
user=admin, password=*****, already created persistence broker instances: 0
15072 DEBUG [Thread-11] sql.SqlGeneratorDefaultImpl - SQL:SELECT DISTINCT
A0.accountID,A0.isDisconnected,A0.serviceType,A0.deleteTag,A0.customerID,A0.
deleteTagSetDate,A0.telephoneNumber,A0.accountNumber,A0.rowVersion,A0.billin
gCycle FROM Account A0 WHERE A0.accountNumber =  ? 
17545 DEBUG [Thread-11] sql.SqlGeneratorDefaultImpl - SQL:SELECT
A0.emailAddress,A0.fname,A0.customerID,A0.faxNumber,A0.roleCode,A0.mname,A0.
rowVersion,A0.lname,A0.passwordClue,A0.loginID,A0.userID,A0.userPassword,A0.
phoneNumber FROM ActiveBillUser A0,Custodian WHERE (Custodian.accountID =  ?
) AND Custodian.userID = A0.userID
17806 DEBUG [Thread-11] sql.SqlGeneratorDefaultImpl - SQL:SELECT
accountID,isDisconnected,serviceType,deleteTag,customerID,deleteTagSetDate,t
elephoneNumber,accountNumber,rowVersion,billingCycle FROM Account WHERE
accountID = ? 
17856 INFO  [Thread-11] core.PersistenceBrokerImpl - Cascade store for this
reference-descriptor (customer) was set to false.
17876 DEBUG [Thread-11] sql.SqlGeneratorDefaultImpl - SQL:INSERT INTO
Account
(accountID,rowVersion,accountNumber,deleteTag,deleteTagSetDate,isDisconnecte
d,telephoneNumber,serviceType,billingCycle,customerID) VALUES
(?,?,?,?,?,?,?,?,?,?) 
18847 ERROR [Thread-11] accesslayer.JdbcAccessImpl - SQLException during the
execution of the insert (for a
com.mobius.activebill.persistentobjects.Account): Violation of UNIQUE KEY
constraint 'IX_AccountNumber'. Cannot insert duplicate key in object
'Account'.
com.jnetdirect.jsql.w: Violation of UNIQUE KEY constraint
'IX_AccountNumber'. Cannot insert duplicate key in object 'Account'.
        at com.jnetdirect.jsql.w.a(Unknown Source)
        at com.jnetdirect.jsql.au.a(Unknown Source)
        at com.jnetdirect.jsql.ag.i(Unknown Source)
        at com.jnetdirect.jsql.ag.new(Unknown Source)
        at com.jnetdirect.jsql.ag.int(Unknown Source)
        at com.jnetdirect.jsql.ap.executeUpdate(Unknown Source)
        at
com.p6spy.engine.logging.P6LogPreparedStatement.executeUpdate(P6LogPreparedS
tatement.java:182)
        at weblogic.jdbc.pool.Statement.executeUpdate(Statement.java:304)
        at
weblogic.jdbc.rmi.internal.PreparedStatementImpl.executeUpdate(PreparedState
mentImpl.java:68)
        at
weblogic.jdbc.rmi.SerialPreparedStatement.executeUpdate(SerialPreparedStatem
ent.java:58)
        at
org.apache.ojb.broker.accesslayer.JdbcAccessImpl.executeInsert(Unknown
Source)
        at
org.apache.ojb.broker.core.PersistenceBrokerImpl.storeToDb(Unknown Source)
        at org.apache.ojb.broker.core.PersistenceBrokerImpl.store(Unknown
Source)
        at org.apache.ojb.broker.core.PersistenceBrokerImpl.store(Unknown
Source)
        at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.store(Unknown Source)
        at dbtests.AccountUpdater.run(AccountUpdater.java:47)
18867 INFO  [Thread-11] accesslayer.ConnectionManagerImpl - Rollback was
called, do rollback on current connection
[EMAIL PROTECTED]
-------------------------------------------------------------------------

The relevant part of repository_user.xml looks like 
<!-- Definitions for Account table -->
   <class-descriptor
          class="com.mobius.activebill.persistentobjects.Account"
          table="Account"
   >
      <field-descriptor
         name="accountID"
         column="accountID"
         jdbc-type="INTEGER"
         primarykey="true"
         autoincrement="true"
      />
      <field-descriptor 
         name="rowVersion" 
         column="rowVersion"
         jdbc-type="INTEGER" 
         locking="true"
      />
      <field-descriptor
         name="accountNumber"
         column="accountNumber"
         jdbc-type="VARCHAR"
      />
       <field-descriptor
         name="deleteTag"
         column="deleteTag"
         jdbc-type="BIT"
      />
      <field-descriptor
         name="deleteTagSetDate"
         column="deleteTagSetDate"
         jdbc-type="TIMESTAMP"
      />
      <field-descriptor
         name="isDisconnected"
         column="isDisconnected"
         jdbc-type="BIT"
      />
      <field-descriptor
         name="telephoneNumber"
         column="telephoneNumber"
         jdbc-type="VARCHAR"
      />   
      <field-descriptor
         name="serviceType"
         column="serviceType"
         jdbc-type="VARCHAR"
      />
      <field-descriptor
         name="billingCycle"
         column="billingCycle"
         jdbc-type="VARCHAR"
      />
      <field-descriptor
         name="customerID"
         column="customerID"
         jdbc-type="INTEGER"
      />
      <reference-descriptor
        name="customer"
        class-ref="com.mobius.activebill.persistentobjects.Customer"
        proxy="true"
        >
        <foreignkey field-ref="customerID"/>
      </reference-descriptor>    
      <collection-descriptor
        name="users"
        element-class-ref="com.mobius.activebill.persistentobjects.User"
        auto-retrieve="true"
        auto-update="false"
        indirection-table="Custodian"
        >
        <fk-pointing-to-this-class column="accountID" />
        <fk-pointing-to-element-class column="userID" />
      </collection-descriptor>
      <collection-descriptor
         name="bills"
         element-class-ref="com.mobius.activebill.persistentobjects.Bill"
         proxy="true"
        >
        <inverse-foreignkey field-ref="accountID"/>
        </collection-descriptor>
   </class-descriptor>
   


thanks,
Bonnie MacKellar

Reply via email to