db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/odmg/collections DListEntry.java DListImpl.java DMapImpl.java DMapEntry.java DSetIterator.java DSetEntry.java DListIterator.java DSetImpl.java DBagImpl.java
Date Sun, 14 Nov 2004 09:37:40 GMT
tomdz       2004/11/14 01:37:40

  Modified:    src/java/org/apache/ojb/odmg/collections DListEntry.java
                        DListImpl.java DMapImpl.java DMapEntry.java
                        DSetIterator.java DSetEntry.java DListIterator.java
                        DSetImpl.java DBagImpl.java
  Log:
  Reworked the OJB core:
  - replaced the factories/configuration concept with the ComponentContainer
  - removal of most static calls within OJB
  - removed the old "D" collection implementations and renamed the new ones
  - moved StatementForClassIF handling to the PersistenceConfiguration
  - moved RowReader caching from the ClassDescriptor to the PersistenceConfiguration
  and other changes (see mail on the dev list for more details)
  
  Revision  Changes    Path
  1.26      +117 -163  db-ojb/src/java/org/apache/ojb/odmg/collections/DListEntry.java
  
  Index: DListEntry.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DListEntry.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- DListEntry.java	4 Apr 2004 23:53:39 -0000	1.25
  +++ DListEntry.java	14 Nov 2004 09:37:40 -0000	1.26
  @@ -1,6 +1,6 @@
   package org.apache.ojb.odmg.collections;
   
  -/* Copyright 2002-2004 The Apache Software Foundation
  +/* Copyright 2003-2004 The Apache Software Foundation
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -15,226 +15,159 @@
    * limitations under the License.
    */
   
  +import org.apache.commons.lang.builder.ToStringBuilder;
   import org.apache.ojb.broker.Identity;
  +import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.PersistenceBrokerFactory;
  -import org.apache.ojb.broker.metadata.FieldDescriptor;
  -import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldFactory;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
  +import org.apache.ojb.broker.util.logging.Logger;
  +import org.apache.ojb.odmg.OJBTxManager;
  +import org.apache.ojb.odmg.TransactionExt;
   import org.apache.ojb.odmg.PBCapsule;
  -import org.apache.ojb.odmg.TransactionImpl;
  -import org.apache.ojb.odmg.TxManagerFactory;
   
   import java.io.Serializable;
   
   /**
  - * Insert the type's description here.
  - * Creation date: (28.01.2001 21:23:26)
  + *
    * @author Thomas Mahler
  + * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
  + * @version $Id$
    */
   public class DListEntry implements Serializable
   {
  -	private static final long serialVersionUID = -3280555665769116060L;
  -    protected int id = 0;
  -    protected int dlistId = 0;
  -    protected DListImpl m_dList;
  -    protected int position;
  +    private static final long serialVersionUID = 5251476492626009907L;
  +    /* declare transient, because ManageableCollection entries need to be {@link java.io.Serializable} */
  +    private transient Logger log;
  +    /* declare transient because the object is not required to be serializable and we can reload it via the oid */
  +    protected transient Object realSubject;
  +    protected DListImpl dList;
  +    protected Integer dlistId;
  +    
  +    protected Integer id;
       protected Identity oid;
  -    protected Object realSubject;
  -    protected PBKey pbKey;
  +    protected int position;
   
       /**
  -     * Insert the method's description here.
  -     * Creation date: (09.02.2001 21:28:01)
  +     * Used to materialize DLists from the database.
        */
       public DListEntry()
  -    {
  -        super();
  -        if (!PersistentFieldFactory.usesAccessorsAndMutators())
  -            id = generateNewId();
  -    }
  -
  -    public DListEntry(PBKey pbKey)
  -    {
  -        this.pbKey = pbKey;
  -        if (!PersistentFieldFactory.usesAccessorsAndMutators())
  -            id = generateNewId();
  -    }
  +    {}
   
       /**
  -     * DListEntry multi-args constructor used by OJB PersistenceBroker
  +     * Standard way to instantiate new entries
        */
  -    public DListEntry(int anId, int aDlistId, int pos, Identity anOid)
  +    public DListEntry(DListImpl theDList, Object theObject)
       {
  -        this.id = anId;
  -        this.dlistId = aDlistId;
  -        this.position = pos;
  -        this.oid = anOid;
  -    }
  +        dList = theDList;
   
  -    /**
  -     * DListEntry constructor comment.
  -     */
  -    public DListEntry(DListImpl theDlist, Object theObject)
  -    {
  -        this.pbKey = theDlist.getPBKey();
  -        this.m_dList = theDlist;
  -        this.dlistId = theDlist.getId();
  -        this.position = theDlist.size();
  -        TransactionImpl tx = TxManagerFactory.instance().getTransaction();
  -        if (tx != null)
  -            oid = new Identity(theObject, tx.getBroker());
  -        else
  +        if (dList != null)
           {
  -        	PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  -            oid = new Identity(theObject, broker);
  -            broker.close();
  +            dlistId  = dList.getId();
  +            position = dList.size();           
           }
  -        this.realSubject = theObject;
  -        if (!PersistentFieldFactory.usesAccessorsAndMutators())
  -            id = generateNewId();
  -    }
  -
  -    public Identity getOid()
  -    {
  -        return oid;
  -    }
  -
  -    public void setOid(Identity oid)
  -    {
  -        this.oid = oid;
  -    }
  -
  -    public PBKey getPBKey()
  -    {
  -        return pbKey;
  +        realSubject = theObject;
       }
   
  -    public void setPBKey(PBKey pbKey)
  +    protected Logger getLog()
       {
  -        this.pbKey = pbKey;
  -    }
  -
  -    /**
  -     * return a unique id
  -     */
  -    protected int generateNewId()
  -    {
  -        PBCapsule capsule = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
  -        try
  +        if (log == null)
           {
  -            PersistenceBroker broker = capsule.getBroker();
  -            FieldDescriptor fld = broker.getClassDescriptor(this.getClass()).getAutoIncrementFields()[0];
  -            Integer val = (Integer) broker.serviceSequenceManager().getUniqueValue(fld);
  -
  -            int result = val.intValue();
  -            return result;
  -        }
  -        catch (Exception e)
  -        {
  -            LoggerFactory.getDefaultLogger().error("DListEntry: Generation of new id failed", e);
  -            throw new PersistenceBrokerException(e);
  -        }
  -        finally
  -        {
  -            capsule.destroy();
  +            log = LoggerFactory.getLogger(DListEntry.class);
           }
  +        return log;
       }
   
  -    /**
  -     * Insert the method's description here.
  -     * Creation date: (06.02.2001 22:06:55)
  -     * @return int
  -     */
  -    public int getPosition()
  +    protected void prepareForPersistency(PersistenceBroker broker)
       {
  -        return position;
  -    }
  -
  -    /**
  -     * Insert the method's description here.
  -     * Creation date: (30.01.2001 22:32:10)
  -     * @return java.lang.Object
  -     */
  -    public Object getRealSubject()
  -    {
  -        try
  +        if (oid == null)
           {
               if (realSubject == null)
               {
  -                materializeRealSubject();
  +                throw new OJBRuntimeException("Identity and real object are 'null' - Can not persist empty entry");
  +            }
  +            else
  +            {
  +                oid = new Identity(realSubject, broker);
               }
           }
  -        catch (Exception e)
  +    }
  +
  +    protected void prepareRealSubject(PersistenceBroker broker)
  +    {
  +        if (oid == null)
           {
  -            LoggerFactory.getDefaultLogger().error("Cannot materialize real subject", e);
  +            throw new OJBRuntimeException("Cannot retrieve real object because its id is not known");
           }
  -        return realSubject;
  +        realSubject = broker.getObjectByIdentity(oid);
       }
   
       /**
  -     * retrieve the real subject from the underlying RDBMS
  +     * Returns the real subject, i.e. the object stored in this entry.
  +     * 
  +     * @param pbKey     The broker key used by the owning collection
  +     * @param txManager The tx manager
  +     * @return The stored object
        */
  -    private void materializeRealSubject() throws PersistenceBrokerException
  +    public Object getRealSubject(PBKey pbKey, OJBTxManager txManager)
       {
  -        PBCapsule capsule = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
  -        try
  +        if (realSubject != null)
           {
  -            realSubject = capsule.getBroker().getObjectByIdentity(oid);
  +            return realSubject;
           }
  -        finally
  +        else
           {
  -            if(capsule != null) capsule.destroy();
  +            TransactionExt tx = (txManager == null ? null : txManager.getTransaction());
  +
  +            if ((tx != null) && tx.isOpen())
  +            {
  +                prepareRealSubject(tx.getBroker());
  +            }
  +            else
  +            {
  +                if (pbKey != null)
  +                {
  +                    PBCapsule capsule = new PBCapsule(pbKey, null);
  +
  +                    try
  +                    {
  +                        prepareRealSubject(capsule.getBroker());
  +                    }
  +                    finally
  +                    {
  +                        capsule.destroy();
  +                    }
  +                }
  +                else
  +                {
  +                    getLog().warn("No tx, no PBKey - can't materialise object with Identity " + getOid());
  +                }
  +            }
           }
  +        return realSubject;
       }
   
  -    /**
  -     * Insert the method's description here.
  -     * Creation date: (06.02.2001 22:06:55)
  -     * @param newPosition int
  -     */
  -    public void setPosition(int newPosition)
  +    public void setRealSubject(Object realSubject)
       {
  -        position = newPosition;
  +        this.realSubject = realSubject;
       }
   
  -    /**
  -     * Insert the method's description here.
  -     * Creation date: (10.02.2001 17:58:45)
  -     * @param realSubject java.lang.Object
  -     */
  -    void setRealSubject(Object realSubject)
  +    public int getPosition()
       {
  -        this.realSubject = realSubject;
  +        return position;
       }
   
  -    /**
  -     * return String representation.
  -     */
  -    public String toString()
  +    public void setPosition(int newPosition)
       {
  -        if (realSubject == null)
  -        {
  -            return oid.toString();
  -        }
  -        else
  -        {
  -            return realSubject.toString();
  -        }
  +        position = newPosition;
       }
   
       /**
        * Gets the dlistId.
        * @return Returns a int
        */
  -    public int getDlistId()
  +    public Integer getDlistId()
       {
  -        if (dlistId == -1)
  -        {
  -            dlistId = this.m_dList != null ? this.m_dList.getId() : generateNewId();
  -        }
           return dlistId;
       }
   
  @@ -242,7 +175,7 @@
        * Sets the dlistId.
        * @param dlistId The dlistId to set
        */
  -    public void setDlistId(int dlistId)
  +    public void setDlistId(Integer dlistId)
       {
           this.dlistId = dlistId;
       }
  @@ -251,12 +184,8 @@
        * Gets the id.
        * @return Returns a int
        */
  -    public int getId()
  +    public Integer getId()
       {
  -        if (id == -1)
  -        {
  -            id = generateNewId();
  -        }
           return id;
       }
   
  @@ -264,8 +193,33 @@
        * Sets the id.
        * @param id The id to set
        */
  -    public void setId(int id)
  +    public void setId(Integer id)
       {
           this.id = id;
  +    }
  +
  +    public Identity getOid()
  +    {
  +        return oid;
  +    }
  +
  +    public void setOid(Identity oid)
  +    {
  +        this.oid = oid;
  +    }
  +
  +    /**
  +     * return String representation.
  +     */
  +    public String toString()
  +    {
  +        ToStringBuilder buf = new ToStringBuilder(this);
  +
  +        buf.append("id", id);
  +        buf.append("dListId", dlistId);
  +        buf.append("position", position);
  +        buf.append("identity", oid);
  +        buf.append("realSubject", realSubject);
  +        return buf.toString();
       }
   }
  
  
  
  1.29      +296 -151  db-ojb/src/java/org/apache/ojb/odmg/collections/DListImpl.java
  
  Index: DListImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DListImpl.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- DListImpl.java	22 Oct 2004 21:54:45 -0000	1.28
  +++ DListImpl.java	14 Nov 2004 09:37:40 -0000	1.29
  @@ -1,6 +1,6 @@
   package org.apache.ojb.odmg.collections;
   
  -/* Copyright 2002-2004 The Apache Software Foundation
  +/* Copyright 2003-2004 The Apache Software Foundation
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -15,97 +15,139 @@
    * limitations under the License.
    */
   
  +import java.util.AbstractList;
  +import java.util.ArrayList;
  +import java.util.Iterator;
  +import java.util.List;
  +import java.util.ListIterator;
  +
  +import org.apache.commons.lang.builder.ToStringBuilder;
   import org.apache.ojb.broker.ManageableCollection;
   import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.PersistenceBrokerAware;
   import org.apache.ojb.broker.PersistenceBrokerException;
  +import org.apache.ojb.broker.PersistenceBrokerInternal;
   import org.apache.ojb.broker.core.ValueContainer;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
  -import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldFactory;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
  +import org.apache.ojb.odmg.OJBTxManager;
   import org.apache.ojb.odmg.PBCapsule;
   import org.apache.ojb.odmg.TransactionImpl;
  -import org.apache.ojb.odmg.TxManagerFactory;
  +import org.apache.ojb.odmg.TransactionExt;
   import org.apache.ojb.odmg.oql.OQLQueryImpl;
   import org.odmg.DArray;
   import org.odmg.DCollection;
   import org.odmg.DList;
   import org.odmg.ODMGRuntimeException;
   import org.odmg.OQLQuery;
  +import org.odmg.QueryInvalidException;
   import org.odmg.Transaction;
   
  -import java.util.Vector;
  -import java.util.Iterator;
  -import java.util.ListIterator;
  -
  -
   
   /**
  - * Insert the type's description here.
  - * Creation date: (10.02.2001 20:50:26)
  + *
    * @author Thomas Mahler
  + * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
    */
  -public class DListImpl extends java.util.AbstractList implements DList, DArray,
  -        ManageableCollection, java.io.Serializable
  +public class DListImpl extends AbstractList implements DList,
  +                                                       DArray,
  +                                                       ManageableCollection,
  +                                                       PersistenceBrokerAware
   {
  -	private static final long serialVersionUID = -835415723401679339L;
  -    private Logger log = LoggerFactory.getLogger(DListImpl.class);
  -    /**
  -     * decide whether we can defer ID generation based on the type of persistentField we are using
  -     * performance hit to generate these ID's is to much if you never use them.
  -     */
  -    private int id = -1;
  -    private Vector elements;
  +    private static final long serialVersionUID = -9219943066614026526L;
  +    /*
  +     * declare transient, because ManageableCollection is {@link java.io.Serializable}.
  +     */
  +    private transient Logger log;
  +    private Integer id;
  +    private List elements;
       private int size;
  +    private OJBTxManager txManager;
       /**
        * PBKey this DList belongs to.
        */
  -    protected PBKey pbKey;
  +    private PBKey pbKey;
   
       /**
  -     * DListImpl constructor comment.
  +     * Empty constructor for creating a list object from the database.
        */
       public DListImpl()
       {
           super();
  -        elements = new Vector();
  -        this.size = 0;
  -        if (!PersistentFieldFactory.usesAccessorsAndMutators())
  -            id = generateNewId();
  +        this.txManager = null;
  +        this.elements  = new ArrayList();
  +        this.size      = 0;
       }
   
       /**
  -     * DListImpl constructor comment.
  +     * Used by PB-Kernel to instantiate ManageableCollections
  +     * FOR INTERNAL USE ONLY
        */
  -    public DListImpl(PBKey pbKey)
  +    public DListImpl(OJBTxManager txManager)
       {
           super();
  -        elements = new Vector();
  -        this.size = 0;
  -        this.pbKey = pbKey;
  -        if (!PersistentFieldFactory.usesAccessorsAndMutators())
  -            id = generateNewId();
  +        this.txManager = txManager;
  +        this.elements  = new ArrayList();
  +        this.size      = 0;
       }
   
  -    /**
  -     * DListImpl constructor comment.
  -     */
  -    public DListImpl(int theId, int theSize)
  +    protected Logger getLog()
       {
  -        super();
  -        elements = new Vector();
  -        id = theId;
  -        this.size = theSize;
  +        if (log == null)
  +        {
  +            log = LoggerFactory.getLogger(getClass());
  +        }
  +        return log;
  +    }
  +
  +    public OJBTxManager getTxManager()
  +    {
  +        return txManager;
  +    }
  +
  +    public void setTxManager(OJBTxManager txManager)
  +    {
  +        this.txManager = txManager;
  +    }
  +
  +    private DListEntry prepareEntry(Object obj)
  +    {
  +        return new DListEntry(this, obj);
  +    }
  +
  +    private boolean checkForOpenTransaction(TransactionExt tx)
  +    {
  +        boolean result = false;
  +
  +        if ((tx != null) && tx.isOpen())
  +        {
  +            result = true;
  +            if (pbKey == null)
  +            {
  +                pbKey = tx.getBroker().getPBKey();
  +            }
  +        }
  +        return result;
       }
   
       public PBKey getPBKey()
       {
  +        if ((pbKey == null) && (txManager != null))
  +        {
  +            TransactionExt tx = txManager.getTransaction();
  +
  +            if (tx != null)
  +            {
  +                checkForOpenTransaction(tx);
  +            }
  +        }
           return pbKey;
       }
   
  @@ -134,31 +176,35 @@
        */
       public void add(int index, Object element)
       {
  +        DListEntry entry = prepareEntry(element);
   
  -        this.size++;
  -        DListEntry entry = new DListEntry(this, element);
           elements.add(index, entry);
  +        size++;
  +
  +        TransactionImpl tx = txManager.getTransaction();
  +
           // if we are in a transaction: acquire locks !
  -        Transaction tx = TxManagerFactory.instance().getTransaction();
  -        if ((tx != null) && (tx.isOpen()))
  +        if (checkForOpenTransaction(tx))
           {
               tx.lock(this, Transaction.WRITE);
               tx.lock(entry, Transaction.WRITE);
               tx.lock(element, Transaction.READ);
  +            entry.prepareForPersistency(tx.getBroker());
           }
   
           // changing the position markers of entries:
           int offset = 0;
  +
           try
           {
  -            offset = ((DListEntry) elements.get(index - 1)).getPosition();
  +            offset = ((DListEntry)elements.get(index - 1)).getPosition();
           }
           catch (Exception ignored)
  -        {
  -        }
  +        {}
  +
           for (int i = offset; i < elements.size(); i++)
           {
  -            entry = (DListEntry) elements.get(i);
  +            entry = (DListEntry)elements.get(i);
               entry.setPosition(i);
           }
       }
  @@ -176,51 +222,55 @@
        * @return the element previously at the specified position.
        *
        * @throws UnsupportedOperationException if the <tt>remove</tt> method is
  -     *		  not supported by this list.
  +     *        not supported by this list.
        * @throws IndexOutOfBoundsException if the specified index is out of
  -     * 		  range (<tt>index &lt; 0 || index &gt;= size()</tt>).
  +     *        range (<tt>index &lt; 0 || index &gt;= size()</tt>).
        */
       public Object remove(int index)
       {
  -        DListEntry entry = (DListEntry) elements.get(index);
  +        DListEntry      entry = (DListEntry)elements.get(index);
  +        TransactionImpl tx    = (txManager == null ? null : txManager.getTransaction());
  +
           // if we are in a transaction: acquire locks !
  -        TransactionImpl tx = TxManagerFactory.instance().getTransaction();
  -        if ((tx != null) && (tx.isOpen()))
  +        if (checkForOpenTransaction(tx))
           {
               tx.markDelete(entry);
           }
  -        this.size--;
  +        size--;
           elements.remove(index);
  +
           // changing the position markers of entries:
           int offset = 0;
  +
           try
           {
  -            offset = ((DListEntry) elements.get(index)).getPosition();
  +            offset = ((DListEntry)elements.get(index)).getPosition();
           }
           catch (Exception ignored)
  -        {
  -        }
  +        {}
           for (int i = offset; i < elements.size(); i++)
           {
  -            entry = (DListEntry) elements.get(i);
  +            entry = (DListEntry)elements.get(i);
               entry.setPosition(i);
           }
   
  -        return entry.getRealSubject();
  +        return entry.getRealSubject(getPBKey(), getTxManager());
       }
   
       /**
        * Creates a new <code>DList</code> object that contains the contents of this
        * <code>DList</code> object concatenated
        * with the contents of the <code>otherList</code> object.
  -     * @param	otherList	The list whose elements are placed at the end of the list
  +     * @param   otherList   The list whose elements are placed at the end of the list
        * returned by this method.
  -     * @return	A new <code>DList</code> that is the concatenation of this list and
  +     * @return  A new <code>DList</code> that is the concatenation of this list and
        * the list referenced by <code>otherList</code>.
        */
  -    public DList concat(org.odmg.DList otherList)
  +    public DList concat(DList otherList)
       {
  -        DListImpl result = new DListImpl(pbKey);
  +        DListImpl result = new DListImpl(txManager);
  +
  +        result.setPBKey(getPBKey());
           result.addAll(this);
           result.addAll(otherList);
           return result;
  @@ -229,44 +279,16 @@
       /**
        * Determines whether there is an element of the collection that evaluates to true
        * for the predicate.
  -     * @param	predicate	An OQL boolean query predicate.
  -     * @return	True if there is an element of the collection that evaluates to true
  +     * @param   predicate   An OQL boolean query predicate.
  +     * @return  True if there is an element of the collection that evaluates to true
        * for the predicate, otherwise false.
  -     * @exception	org.odmg.QueryInvalidException	The query predicate is invalid.
  +     * @exception   org.odmg.QueryInvalidException  The query predicate is invalid.
        */
       public boolean existsElement(String predicate) throws org.odmg.QueryInvalidException
       {
  -        DList results = (DList) this.query(predicate);
  -        if (results == null || results.size() == 0)
  -            return false;
  -        else
  -            return true;
  +        DList results = (DList)query(predicate);
   
  -    }
  -
  -    /**
  -     * return a unique id for PK
  -     */
  -    protected int generateNewId()
  -    {
  -        PBCapsule capsule = new PBCapsule(null, TxManagerFactory.instance().getTransaction());
  -        try
  -        {
  -            PersistenceBroker broker = capsule.getBroker();
  -            FieldDescriptor fld = broker.getClassDescriptor(this.getClass()).getAutoIncrementFields()[0];
  -            Integer val = (Integer) broker.serviceSequenceManager().getUniqueValue(fld);
  -            int result = val.intValue();
  -            return result;
  -        }
  -        catch (Exception e)
  -        {
  -            log.error("Generation of new id failed", e);
  -            throw new PersistenceBrokerException(e);
  -        }
  -        finally
  -        {
  -            capsule.destroy();
  -        }
  +        return (results != null) && (results.size() > 0);
       }
   
       /**
  @@ -280,8 +302,9 @@
        */
       public Object get(int index)
       {
  -        DListEntry entry = (DListEntry) elements.get(index);
  -        return entry.getRealSubject();
  +        DListEntry entry = (DListEntry)elements.get(index);
  +
  +        return entry.getRealSubject(getPBKey(), getTxManager());
       }
   
       /**
  @@ -289,7 +312,7 @@
        * Creation date: (10.02.2001 20:53:01)
        * @return java.util.Vector
        */
  -    public Vector getElements()
  +    public List getElements()
       {
           return elements;
       }
  @@ -298,12 +321,8 @@
        * Lazily return the Id, no point in precomputing it.
        * @return int
        */
  -    public int getId()
  +    public Integer getId()
       {
  -        if (id == -1)
  -        {
  -            id = generateNewId();
  -        }
           return id;
       }
   
  @@ -357,26 +376,31 @@
           try
           {
               Criteria crit = null;
  +
               for (int i = 0; i < elements.size(); i++)
               {
  -                DListEntry entry = (DListEntry) elements.get(i);
  -                Object obj = entry.getRealSubject();
  -                ClassDescriptor cld = brokerForClass.getClassDescriptor(obj.getClass());
  -
  +                DListEntry        entry    = (DListEntry)elements.get(i);
  +                Object            obj      = entry.getRealSubject(getPBKey(), getTxManager());
  +                ClassDescriptor   cld      = brokerForClass.getClassDescriptor(obj.getClass());
                   FieldDescriptor[] pkFields = cld.getPkFields();
  -                ValueContainer[] pkValues = brokerForClass.serviceBrokerHelper().getKeyValues(cld, obj);
  +                ValueContainer[]  pkValues = brokerForClass.serviceBrokerHelper().getKeyValues(cld, obj);
  +                Criteria          criteria = new Criteria();
   
  -                Criteria criteria = new Criteria();
                   for (int j = 0; j < pkFields.length; j++)
                   {
                       FieldDescriptor fld = pkFields[j];
  +
                       criteria.addEqualTo(fld.getPersistentField().getName(), pkValues[j].getValue());
                   }
   
                   if (crit == null)
  +                {
                       crit = criteria;
  +                }
                   else
  +                {
                       crit.addOrCriteria(criteria);
  +                }
               }
               return crit;
           }
  @@ -389,67 +413,85 @@
       private Class getElementsExtentClass(PersistenceBroker brokerForClass) throws PersistenceBrokerException
       {
           // we ll have to compute the most general extent class here !!!
  -        DListEntry entry = (DListEntry) elements.get(0);
  -        Class elementsClass = entry.getRealSubject().getClass();
  -        Class extentClass = brokerForClass.getTopLevelClass(elementsClass);
  -        return extentClass;
  +        DListEntry entry         = (DListEntry)elements.get(0);
  +        Class      elementsClass = entry.getRealSubject(getPBKey(), getTxManager()).getClass();
  +
  +        return brokerForClass.getTopLevelClass(elementsClass);
       }
   
       /**
        * Evaluate the boolean query predicate for each element of the collection and
        * return a new collection that contains each element that evaluated to true.
  -     * @param	predicate	An OQL boolean query predicate.
  -     * @return	A new collection containing the elements that evaluated true for the predicate.
  -     * @exception	org.odmg.QueryInvalidException	The query predicate is invalid.
  +     * @param   predicate   An OQL boolean query predicate.
  +     * @return  A new collection containing the elements that evaluated true for the predicate.
  +     * @exception   org.odmg.QueryInvalidException  The query predicate is invalid.
        */
  -    public DCollection query(String predicate) throws org.odmg.QueryInvalidException
  +    public DCollection query(String predicate) throws QueryInvalidException
       {
           // 1.build complete OQL statement
           String oql = "select all from java.lang.Object where " + predicate;
  -        /* @todo Use a ObjectFactory to instantiate OQLQuery? */
  -        OQLQuery predicateQuery = new OQLQueryImpl(pbKey);
  +
  +        // TODO: Use a ObjectFactory to instantiate OQLQuery?
  +        OQLQuery predicateQuery = new OQLQueryImpl(pbKey, txManager, getClass());
  +
           predicateQuery.create(oql);
  -        Query pQ = ((OQLQueryImpl) predicateQuery).getQuery();
  -        Criteria pCrit = pQ.getCriteria();
   
  -        PBCapsule handle = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
  -        DList result;
  +        Query          pQ    = ((OQLQueryImpl) predicateQuery).getQuery();
  +        Criteria       pCrit = pQ.getCriteria();
  +        TransactionExt tx    = txManager.getTransaction();
  +
  +        if (tx == null)
  +        {
  +            throw new QueryInvalidException("Need running transaction to do query");
  +        }
  +
  +        PBCapsule handle = new PBCapsule(pbKey, tx);
  +        DList     result;
   
           try
           {
  -            PersistenceBroker broker = handle.getBroker();
  +            PersistenceBroker broker              = handle.getBroker();
  +            Criteria          allElementsCriteria = getPkCriteriaForAllElements(broker);
   
  -            Criteria allElementsCriteria = this.getPkCriteriaForAllElements(broker);
               // join selection of elements with predicate criteria:
               allElementsCriteria.addAndCriteria(pCrit);
   
               Class clazz = null;
  +
               try
               {
  -                clazz = this.getElementsExtentClass(broker);
  +                clazz = getElementsExtentClass(broker);
               }
               catch (PersistenceBrokerException e)
               {
                   throw new ODMGRuntimeException(e.getMessage());
               }
  +
               Query q = new QueryByCriteria(clazz, allElementsCriteria);
  -            if (log.isDebugEnabled()) log.debug(q.toString());
  +
  +            if (getLog().isDebugEnabled())
  +            {
  +                getLog().debug(q.toString());
  +            }
   
               result = null;
               try
               {
  -                result = (DListImpl) broker.getCollectionByQuery(DListImpl.class, q);
  +                result = (DList)broker.getCollectionByQuery(DListImpl.class, q);
               }
               catch (PersistenceBrokerException e)
               {
  -                log.error("Query failed", e);
  +                getLog().error("Query failed", e);
                   throw new OJBRuntimeException(e);
               }
           }
           finally
           {
               // cleanup stuff
  -            if (handle != null) handle.destroy();
  +            if (handle != null)
  +            {
  +                handle.destroy();
  +            }
           }
   
           // 3. return resulting collection
  @@ -457,29 +499,61 @@
   
       }
   
  +    public int hashCode()
  +    {
  +        int hashCode = 1;
  +
  +        for (Iterator it = elements.iterator(); it.hasNext();)
  +        {
  +            Object obj = it.next();
  +
  +            hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
  +        }
  +        return hashCode;
  +    }
  +
  +    public String toString()
  +    {
  +        ToStringBuilder buf = new ToStringBuilder(this);
  +
  +        buf.append("id", id);
  +        buf.append("pbKey", pbKey);
  +        buf.append("size", size);
  +        buf.append("[containing elements: ");
  +
  +        for (Iterator it = elements.iterator(); it.hasNext();)
  +        {
  +            Object obj = it.next();
  +
  +            buf.append(obj != null ? obj.toString() : null);
  +        }
  +        buf.append("]");
  +        return buf.toString();
  +    }
  +
       /**
        * Access all of the elements of the collection that evaluate to true for the
        * provided query predicate.
  -     * @param	predicate	An OQL boolean query predicate.
  -     * @return	An iterator used to iterate over the elements that evaluated true for the predicate.
  -     * @exception	org.odmg.QueryInvalidException	The query predicate is invalid.
  +     * @param   predicate   An OQL boolean query predicate.
  +     * @return  An iterator used to iterate over the elements that evaluated true for the predicate.
  +     * @exception   org.odmg.QueryInvalidException  The query predicate is invalid.
        */
       public Iterator select(String predicate) throws org.odmg.QueryInvalidException
       {
  -        return this.query(predicate).iterator();
  +        return query(predicate).iterator();
       }
   
       /**
        * Selects the single element of the collection for which the provided OQL query
        * predicate is true.
  -     * @param	predicate	An OQL boolean query predicate.
  +     * @param   predicate   An OQL boolean query predicate.
        * @return The element that evaluates to true for the predicate. If no element
        * evaluates to true, null is returned.
  -     * @exception	org.odmg.QueryInvalidException	The query predicate is invalid.
  +     * @exception   org.odmg.QueryInvalidException  The query predicate is invalid.
        */
       public Object selectElement(String predicate) throws org.odmg.QueryInvalidException
       {
  -        return ((DList) this.query(predicate)).get(0);
  +        return ((DList)query(predicate)).get(0);
       }
   
       /**
  @@ -499,10 +573,12 @@
        * from the database. Thus it is is save to cast anObject to the underlying element type of the
        * collection.
        */
  -    public void ojbAdd(java.lang.Object anObject)
  +    public void ojbAdd(Object anObject)
       {
  -        this.size++;
  -        DListEntry entry = new DListEntry(this, anObject);
  +        size++;
  +
  +        DListEntry entry = prepareEntry(anObject);
  +
           entry.setPosition(elements.size());
           elements.add(entry);
       }
  @@ -511,12 +587,11 @@
        * adds a Collection to this collection. Used in reading Extents from the Database.
        * Thus it is save to cast otherCollection to this.getClass().
        */
  -    public void ojbAddAll(org.apache.ojb.broker.ManageableCollection otherCollection)
  +    public void ojbAddAll(ManageableCollection otherCollection)
       {
           // don't use this to avoid locking
  -        // this.addAll((DListImpl) otherCollection);
  -        Iterator it = otherCollection.ojbIterator();
  -        while (it.hasNext())
  +        // this.addAll((DListImpl_2) otherCollection);
  +        for (Iterator it = otherCollection.ojbIterator(); it.hasNext();)
           {
               ojbAdd(it.next());
           }
  @@ -526,14 +601,14 @@
        * returns an Iterator over all elements in the collection. Used during store and delete Operations.
        * If the implementor does not return an iterator over ALL elements, OJB cannot store and delete all elements properly.
        */
  -    public java.util.Iterator ojbIterator()
  +    public Iterator ojbIterator()
       {
  -        return this.iterator();
  +        return iterator();
       }
   
       /**
        * Resize the array to have <code>newSize</code> elements.
  -     * @param	newSize	The new size of the array.
  +     * @param   newSize The new size of the array.
        */
       public void resize(int newSize)
       {
  @@ -543,7 +618,7 @@
        * Sets the elements.
        * @param elements The elements to set
        */
  -    public void setElements(Vector elements)
  +    public void setElements(List elements)
       {
           this.elements = elements;
       }
  @@ -552,7 +627,7 @@
        * Sets the id.
        * @param id The id to set
        */
  -    public void setId(int id)
  +    public void setId(Integer id)
       {
           this.id = id;
       }
  @@ -573,5 +648,75 @@
       public void setSize(int size)
       {
           this.size = size;
  +    }
  +
  +
  +    //***************************************************************
  +    // PersistenceBrokerAware interface
  +    //***************************************************************
  +
  +    /**
  +     * prepare itself for persistence. Each DList entry generates an
  +     * {@link org.apache.ojb.broker.Identity} for the wrapped persistent
  +     * object.
  +     */
  +    public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +        for (Iterator it = elements.iterator(); it.hasNext();)
  +        {
  +            ((DListEntry)it.next()).prepareForPersistency(broker);
  +        }
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +        if (pbKey == null)
  +        {
  +            pbKey = broker.getPBKey();
  +        }
  +        if (txManager == null)
  +        {
  +            // TODO: this is something of a is a hack, but currently the only way to get the current
  +            //       tx manager to a collection retrieved via Database#lookup or via a PB read
  +            txManager = (OJBTxManager)((PersistenceBrokerInternal)broker).getConfiguration().getComponentContainer().getSingletonInstance(OJBTxManager.class);
  +        }
       }
   }
  
  
  
  1.22      +129 -73   db-ojb/src/java/org/apache/ojb/odmg/collections/DMapImpl.java
  
  Index: DMapImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DMapImpl.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- DMapImpl.java	4 Apr 2004 23:53:39 -0000	1.21
  +++ DMapImpl.java	14 Nov 2004 09:37:40 -0000	1.22
  @@ -21,14 +21,13 @@
   
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.PersistenceBrokerAware;
   import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.metadata.FieldDescriptor;
  -import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldFactory;
  +import org.apache.ojb.broker.PersistenceBrokerInternal;
   import org.apache.ojb.broker.util.collections.ManageableHashSet;
  -import org.apache.ojb.broker.util.logging.LoggerFactory;
  -import org.apache.ojb.odmg.PBCapsule;
  +import org.apache.ojb.odmg.OJBTxManager;
  +import org.apache.ojb.odmg.TransactionExt;
   import org.apache.ojb.odmg.TransactionImpl;
  -import org.apache.ojb.odmg.TxManagerFactory;
   import org.odmg.DMap;
   import org.odmg.Transaction;
   
  @@ -37,44 +36,38 @@
    * @version $Id$
    */
   
  -public class DMapImpl extends AbstractMap implements DMap, Serializable
  +public class DMapImpl extends AbstractMap implements DMap, PersistenceBrokerAware, Serializable
   {
   	private static final long serialVersionUID = 7048246616243056480L;
  -    private int id = -1;
  +    private Integer id;
       private ManageableHashSet entries;
       private int size = 0;
  +    private OJBTxManager txManager;
       private PBKey pbKey;
   
       /**
  -     * DMapImpl constructor comment.
  +     * Empty constructor for creating a map object from the database.
        */
       public DMapImpl()
       {
  -        this.entries = new ManageableHashSet();
  -		if (!PersistentFieldFactory.usesAccessorsAndMutators())
  -	        id = generateNewId();
  +        this.txManager = null;
  +        this.entries   = new ManageableHashSet();
       }
   
       /**
  -     * DListImpl constructor comment.
  +     * Internally-used constructor.
        */
  -    public DMapImpl(PBKey key)
  +    public DMapImpl(OJBTxManager txManager)
       {
  -        this.entries = new ManageableHashSet();
  -        this.pbKey = key;
  -		if (!PersistentFieldFactory.usesAccessorsAndMutators())
  -	        id = generateNewId();
  +        this();
  +        this.txManager = txManager;
       }
   
  -    /**
  -     * DMapImpl constructor comment.
  -     */
  -    public DMapImpl(int theId, int theSize)
  +    protected OJBTxManager getTxManager()
       {
  -        id = theId;
  -        this.size = theSize;
  +        return txManager;
       }
  -
  +    
       public PBKey getPBKey()
       {
           return pbKey;
  @@ -85,6 +78,21 @@
           this.pbKey = pbKey;
       }
   
  +    private boolean checkForOpenTransaction(TransactionExt tx)
  +    {
  +        boolean result = false;
  +
  +        if ((tx != null) && tx.isOpen())
  +        {
  +            result = true;
  +            if (pbKey == null)
  +            {
  +                pbKey = tx.getBroker().getPBKey();
  +            }
  +        }
  +        return result;
  +    }
  +
       /**
        * Returns a set view of the mappings contained in this map.  Each element
        * in the returned set is a <tt>Map.Entry</tt>.  The set is backed by the
  @@ -104,41 +112,10 @@
       }
   
       /**
  -     * return a unique id
  -     */
  -    protected int generateNewId()
  -    {
  -        PBCapsule capsule = new PBCapsule(null, TxManagerFactory.instance().getTransaction());
  -        try
  -        {
  -            PersistenceBroker broker = capsule.getBroker();
  -            FieldDescriptor fld = broker.getClassDescriptor(this.getClass()).getAutoIncrementFields()[0];
  -            Integer val = (Integer) broker.serviceSequenceManager().getUniqueValue(fld);
  -
  -            int result = val.intValue();
  -            return result;
  -        }
  -        catch (Exception e)
  -        {
  -            LoggerFactory.getDefaultLogger().error("DMapImpl: Generation of new id failed", e);
  -            throw new PersistenceBrokerException(e);
  -        }
  -        finally
  -        {
  -            capsule.destroy();
  -        }
  -    }
  -
  -
  -    /**
        * lazily retrieve the ID of the set, no need to precompute it.
        */
  -    public int getId()
  +    public Integer getId()
       {
  -    	if (id == -1)
  -    	{
  -    		id = generateNewId();
  -    	}
           return id;
       }
   
  @@ -147,13 +124,13 @@
        */
       public Object put(Object key, Object value)
       {
  -
           DMapEntry entry = new DMapEntry(this, key, value);
  -        boolean ok = entries.add(entry);
  -        if (ok)
  +
  +        if (entries.add(entry))
           {
  -            TransactionImpl tx = TxManagerFactory.instance().getTransaction();
  -            if ((tx != null) && (tx.isOpen()))
  +            TransactionImpl tx = (txManager == null ? null : txManager.getTransaction());
  +
  +            if ((tx != null) && tx.isOpen())
               {
                   tx.lock(this, Transaction.WRITE);
                   tx.lock(entry, Transaction.WRITE);
  @@ -164,41 +141,52 @@
           }
           else
           {
  -            return this.get(key);
  +            return get(key);
           }
       }
   
   
       public Object remove(Object key)
       {
  -        Iterator i = entrySet().iterator();
  +        Iterator  i            = entrySet().iterator();
           DMapEntry correctEntry = null;
  +
           if (key == null)
           {
  -            while (correctEntry == null && i.hasNext())
  +            while ((correctEntry == null) && i.hasNext())
               {
  -                DMapEntry e = (DMapEntry) i.next();
  -                if (e.getKey() == null)
  +                DMapEntry e = (DMapEntry)i.next();
  +
  +                if (e.getRealKey(getPBKey(), getTxManager()) == null)
  +                {
                       correctEntry = e;
  +                }
               }
           }
           else
           {
  -            while (correctEntry == null && i.hasNext())
  +            while ((correctEntry == null) && i.hasNext())
               {
  -                DMapEntry e = (DMapEntry) i.next();
  -                if (key.equals(e.getKey()))
  +                DMapEntry e = (DMapEntry)i.next();
  +
  +                if (key.equals(e.getRealKey(getPBKey(), getTxManager())))
  +                {
                       correctEntry = e;
  +                }
               }
           }
   
           Object oldValue = null;
  +
           if (correctEntry != null)
           {
  +            TransactionImpl tx = (txManager == null ? null : txManager.getTransaction());
  +
               oldValue = correctEntry.getValue();
               i.remove();
  -            TransactionImpl tx = TxManagerFactory.instance().getTransaction();
  -            if ((tx != null) && (tx.isOpen()))
  +
  +            // if we are in a transaction: acquire locks !
  +            if (checkForOpenTransaction(tx))
               {
                   tx.markDelete(correctEntry);
               }
  @@ -229,7 +217,7 @@
        * Sets the id.
        * @param id The id to set
        */
  -    public void setId(int id)
  +    public void setId(Integer id)
       {
           this.id = id;
       }
  @@ -252,4 +240,72 @@
           this.size = size;
       }
   
  +    //***************************************************************
  +    // PersistenceBrokerAware interface
  +    //***************************************************************
  +
  +    /**
  +     * prepare itself for persistence. Each DList entry generates an
  +     * {@link org.apache.ojb.broker.Identity} for the wrapped persistent
  +     * object.
  +     */
  +    public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +        for (Iterator it = entries.iterator(); it.hasNext();)
  +        {
  +            ((DMapEntry)it.next()).prepareForPersistency(broker);
  +        }
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +        if (pbKey == null)
  +        {
  +            pbKey = broker.getPBKey();
  +        }
  +        if (txManager == null)
  +        {
  +            // TODO: this is something of a is a hack, but currently the only way to get the current
  +            //       tx manager to a collection retrieved via Database#lookup or via a PB read
  +            txManager = (OJBTxManager)((PersistenceBrokerInternal)broker).getConfiguration().getComponentContainer().getSingletonInstance(OJBTxManager.class);
  +        }
  +    }
   }
  
  
  
  1.19      +185 -104  db-ojb/src/java/org/apache/ojb/odmg/collections/DMapEntry.java
  
  Index: DMapEntry.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DMapEntry.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- DMapEntry.java	4 Apr 2004 23:53:39 -0000	1.18
  +++ DMapEntry.java	14 Nov 2004 09:37:40 -0000	1.19
  @@ -17,171 +17,252 @@
    */
   
   import java.io.Serializable;
  +import java.util.Map.Entry;
   
   import org.apache.ojb.broker.Identity;
  +import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PBKey;
  -import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.PersistenceBrokerFactory;
  -import org.apache.ojb.broker.metadata.FieldDescriptor;
  -import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldFactory;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
  +import org.apache.ojb.odmg.OJBTxManager;
   import org.apache.ojb.odmg.PBCapsule;
  -import org.apache.ojb.odmg.TransactionImpl;
  -import org.apache.ojb.odmg.TxManagerFactory;
  -import org.odmg.Transaction;
  +import org.apache.ojb.odmg.TransactionExt;
   
   /**
    * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
    * @version $Id$
    */
   
  -public class DMapEntry implements java.util.Map.Entry, Serializable
  +public class DMapEntry implements Entry, Serializable
   {
   	private static final long serialVersionUID = 4382757889982004339L;
  -    private Logger log = LoggerFactory.getLogger(DMapEntry.class);
  -    private int id;
  -    private int dmapId;
  -    private Identity keyOID;
  -    private Object keyRealSubject;
  -    private Identity valueOID;
  -    private Object valueRealSubject;
  -    private PBKey pbKey;
  +    private transient Logger log = LoggerFactory.getLogger(DMapEntry.class);
  +
  +    private DMapImpl dmap;
  +    private Integer dmapId;
  +    private Integer id;
  +    private Identity keyOid;
  +    /* declare transient because the object is not required to be serializable and we can reload it via the oid */
  +    private transient Object keyRealSubject;
  +    private Identity valueOid;
  +    /* declare transient because the object is not required to be serializable and we can reload it via the oid */
  +    private transient Object valueRealSubject;
   
       /**
  -     * DMapEntry constructor comment.
  +     * Used to materialize DMaps from the database.
        */
       public DMapEntry()
  -    {
  -        super();
  -    }
  -
  -    public DMapEntry(PBKey key)
  -    {
  -        this.pbKey = key;
  -    }
  +    {}
   
       /**
        * DMapEntry constructor comment.
        */
  -    public DMapEntry(int id, int mapId, Identity keyOID, Identity valueOID)
  +    public DMapEntry(DMapImpl map, Object key, Object value)
       {
  -        this.id = id;
  -        this.dmapId = mapId;
  -        this.keyOID = keyOID;
  -        this.valueOID = valueOID;
  +        dmap = map;
  +        if (map != null)
  +        {
  +            dmapId = map.getId();
  +        }
  +        keyRealSubject   = key;
  +        valueRealSubject = value;
       }
   
  -    /**
  -     * DMapEntry constructor comment.
  -     */
  -    public DMapEntry(DMapImpl map, Object key, Object value)
  +    protected Logger getLog()
       {
  -        this.pbKey = map.getPBKey();
  -		if (!PersistentFieldFactory.usesAccessorsAndMutators())
  -	        id = generateNewId();
  -        this.dmapId = map.getId();
  -        this.keyRealSubject = key;
  -        PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  -        this.keyOID = new Identity(key,broker);
  -        this.valueRealSubject = value;
  -        this.valueOID = new Identity(value,broker);
  -        broker.close();
  +        if (log == null)
  +        {
  +            log = LoggerFactory.getLogger(DListEntry.class);
  +        }
  +        return log;
       }
   
  -    /**
  -     * return a unique id
  -     */
  -    protected int generateNewId()
  +    protected void prepareForPersistency(PersistenceBroker broker)
       {
  -        PBCapsule capsule = new PBCapsule(null, TxManagerFactory.instance().getTransaction());
  -        try
  +        if (keyOid == null)
           {
  -            PersistenceBroker broker = capsule.getBroker();
  -            FieldDescriptor fld = broker.getClassDescriptor(this.getClass()).getAutoIncrementFields()[0];
  -            Integer val = (Integer) broker.serviceSequenceManager().getUniqueValue(fld);
  -
  -            int result = val.intValue();
  -            return result;
  +            if (keyRealSubject == null)
  +            {
  +                throw new OJBRuntimeException("Key identity and real key object are 'null' - Can not persist empty entry");
  +            }
  +            else
  +            {
  +                keyOid = new Identity(keyRealSubject, broker);
  +            }
  +        }
  +        if (valueOid == null)
  +        {
  +            if (valueRealSubject == null)
  +            {
  +                throw new OJBRuntimeException("Key identity and real key object are 'null' - Can not persist empty entry");
  +            }
  +            else
  +            {
  +                valueOid = new Identity(valueRealSubject, broker);
  +            }
           }
  -        catch (Exception e)
  +    }
  +
  +    protected void prepareKeyRealSubject(PersistenceBroker broker)
  +    {
  +        if (keyOid == null)
           {
  -            LoggerFactory.getDefaultLogger().error("DMapEntry: Generation of new id failed", e);
  -            throw new PersistenceBrokerException(e);
  +            throw new OJBRuntimeException("Cannot retrieve real key object because its id is not known");
           }
  -        finally
  +        keyRealSubject = broker.getObjectByIdentity(keyOid);
  +    }
  +
  +    protected void prepareValueRealSubject(PersistenceBroker broker)
  +    {
  +        if (valueOid == null)
           {
  -            capsule.destroy();
  +            throw new OJBRuntimeException("Cannot retrieve real key object because its id is not known");
           }
  +        valueRealSubject = broker.getObjectByIdentity(valueOid);
       }
   
       /**
  -     * getKey method comment.
  +     * Returns the real key object.
  +     * 
  +     * @param pbKey     The broker key used by the owning collection
  +     * @param txManager The tx manager
  +     * @return The key object
        */
  -    public Object getKey()
  +    public Object getRealKey(PBKey pbKey, OJBTxManager txManager)
       {
  -
  -        if (keyRealSubject == null)
  +        if (keyRealSubject != null)
           {
  -            try
  +            return keyRealSubject;
  +        }
  +        else
  +        {
  +            TransactionExt tx = (txManager == null ? null : txManager.getTransaction());
  +
  +            if ((tx != null) && tx.isOpen())
               {
  -                PBCapsule capsule = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
  -                keyRealSubject = capsule.getBroker().getObjectByIdentity(keyOID);
  -                capsule.destroy();
  +                prepareKeyRealSubject(tx.getBroker());
               }
  -            catch (Exception e)
  +            else
               {
  -                log.error("Could not materialize key with keyOID " + keyOID, e);
  +                if (pbKey != null)
  +                {
  +                    PBCapsule capsule = new PBCapsule(pbKey, null);
  +
  +                    try
  +                    {
  +                        prepareKeyRealSubject(capsule.getBroker());
  +                    }
  +                    finally
  +                    {
  +                        capsule.destroy();
  +                    }
  +                }
  +                else
  +                {
  +                    getLog().warn("No tx, no PBKey - can't materialise key with Identity " + getKeyOid());
  +                }
               }
           }
           return keyRealSubject;
       }
   
  -    /**
  -     * getValue method comment.
  +    /* (non-Javadoc)
  +     * @see java.util.Map.Entry#getKey()
        */
  -    public Object getValue()
  +    public Object getKey()
       {
  +        // we don't save the key object itself but only its identiy
  +        // so we now might have to load the object from the db
  +        if ((keyRealSubject == null) && (dmap != null))
  +        {
  +            return getRealKey(dmap.getPBKey(), dmap.getTxManager());
  +        }
  +        else
  +        {
  +            return keyRealSubject;
  +        }
  +    }
   
  -        if (valueRealSubject == null)
  +    /**
  +     * Returns the real value object.
  +     * 
  +     * @param pbKey     The broker key used by the owning collection
  +     * @param txManager The tx manager
  +     * @return The value object
  +     */
  +    public Object getRealValue(PBKey pbKey, OJBTxManager txManager)
  +    {
  +        if (valueRealSubject != null)
  +        {
  +            return valueRealSubject;
  +        }
  +        else
           {
  -            try
  +            TransactionExt tx = (txManager == null ? null : txManager.getTransaction());
  +
  +            if ((tx != null) && tx.isOpen())
               {
  -                PBCapsule capsule = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
  -                valueRealSubject = capsule.getBroker().getObjectByIdentity(valueOID);
  -                capsule.destroy();
  +                prepareValueRealSubject(tx.getBroker());
               }
  -            catch (Exception e)
  +            else
               {
  -                log.error("Could not materialize value with valueOID " + valueOID, e);
  +                if (pbKey != null)
  +                {
  +                    PBCapsule capsule = new PBCapsule(pbKey, null);
  +
  +                    try
  +                    {
  +                        prepareValueRealSubject(capsule.getBroker());
  +                    }
  +                    finally
  +                    {
  +                        capsule.destroy();
  +                    }
  +                }
  +                else
  +                {
  +                    getLog().warn("No tx, no PBKey - can't materialise value with Identity " + getKeyOid());
  +                }
               }
           }
           return valueRealSubject;
       }
   
  -    /**
  -     * setValue method comment.
  +    /* (non-Javadoc)
  +     * @see java.util.Map.Entry#getValue()
        */
  -    public Object setValue(Object obj)
  +    public Object getValue()
       {
  -        TransactionImpl tx = TxManagerFactory.instance().getTransaction();
  -        if (tx != null)
  +        // we don't save the value object itself but only its identiy
  +        // so we now might have to load the object from the db
  +        if ((valueRealSubject == null) && (dmap != null))
           {
  -            tx.lock(this, Transaction.WRITE);
  +            return getRealValue(dmap.getPBKey(), dmap.getTxManager());
           }
  +        else
  +        {
  +            return valueRealSubject;
  +        }
  +    }
  +
  +    /*
  +     * (non-Javadoc)
  +     * @see java.util.Map.Entry#setValue(java.lang.Object)
  +     */
  +    public Object setValue(Object obj)
  +    {
  +        Object old = valueRealSubject;
  +
           valueRealSubject = obj;
  -        PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  -        valueOID = new Identity(obj,broker);
  -        broker.close();
  -        return obj;
  +        return old;
       }
   
       /**
        * Gets the dmapId.
        * @return Returns a int
        */
  -    public int getDmapId()
  +    public Integer getDmapId()
       {
           return dmapId;
       }
  @@ -190,7 +271,7 @@
        * Sets the dmapId.
        * @param dmapId The dmapId to set
        */
  -    public void setDmapId(int dmapId)
  +    public void setDmapId(Integer dmapId)
       {
           this.dmapId = dmapId;
       }
  @@ -199,7 +280,7 @@
        * Gets the id.
        * @return Returns a int
        */
  -    public int getId()
  +    public Integer getId()
       {
           return id;
       }
  @@ -208,28 +289,28 @@
        * Sets the id.
        * @param id The id to set
        */
  -    public void setId(int id)
  +    public void setId(Integer id)
       {
           this.id = id;
       }
   
  -    public Identity getKeyOID()
  +    public Identity getKeyOid()
       {
  -        return keyOID;
  +        return keyOid;
       }
   
  -    public void setKeyOID(Identity keyOID)
  +    public void setKeyOid(Identity keyOid)
       {
  -        this.keyOID = keyOID;
  +        this.keyOid = keyOid;
       }
   
  -    public Identity getValueOID()
  +    public Identity getValueOid()
       {
  -        return valueOID;
  +        return valueOid;
       }
   
  -    public void setValueOID(Identity valueOID)
  +    public void setValueOid(Identity valueOid)
       {
  -        this.valueOID = valueOID;
  +        this.valueOid = valueOid;
       }
   }
  
  
  
  1.9       +10 -6     db-ojb/src/java/org/apache/ojb/odmg/collections/DSetIterator.java
  
  Index: DSetIterator.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DSetIterator.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DSetIterator.java	4 Apr 2004 23:53:39 -0000	1.8
  +++ DSetIterator.java	14 Nov 2004 09:37:40 -0000	1.9
  @@ -18,7 +18,6 @@
   import java.util.Iterator;
   
   import org.apache.ojb.odmg.TransactionImpl;
  -import org.apache.ojb.odmg.TxManagerFactory;
   
   /**
    * Insert the type's description here.
  @@ -28,14 +27,16 @@
   public class DSetIterator implements Iterator
   {
       private Iterator iter;
  -    private DSetEntry currentEntry = null;
  +    private DSetImpl dset;
  +    private DSetEntry currentEntry;
   
       /**
        * DListIterator constructor comment.
        */
       public DSetIterator(DSetImpl set)
       {
  -        this.iter = set.getElements().iterator();
  +        dset = set;
  +        iter = set.getElements().iterator();
       }
   
       /**
  @@ -58,8 +59,9 @@
        */
       public Object next()
       {
  -        currentEntry = ((DSetEntry) iter.next());
  -        return currentEntry.getRealSubject();
  +        currentEntry = ((DSetEntry)iter.next());
  +
  +        return currentEntry.getRealSubject(dset.getPBKey(), dset.getTxManager());
       }
   
       /**
  @@ -81,7 +83,9 @@
       public void remove()
       {
           iter.remove();
  -        TransactionImpl tx = TxManagerFactory.instance().getTransaction();
  +
  +        TransactionImpl tx = dset.getTxManager().getTransaction();
  +
           if (tx != null)
           {
               tx.markDelete(currentEntry);
  
  
  
  1.11      +4 -29     db-ojb/src/java/org/apache/ojb/odmg/collections/DSetEntry.java
  
  Index: DSetEntry.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DSetEntry.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DSetEntry.java	4 Apr 2004 23:53:39 -0000	1.10
  +++ DSetEntry.java	14 Nov 2004 09:37:40 -0000	1.11
  @@ -15,11 +15,6 @@
    * limitations under the License.
    */
   
  -import org.apache.ojb.broker.Identity;
  -import org.apache.ojb.broker.PBKey;
  -import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.PersistenceBrokerFactory;
  -
   /**
    * Insert the type's description here.
    * Creation date: (26.02.2001 13:18:35)
  @@ -30,30 +25,13 @@
       private static final long serialVersionUID = 6334656303221694908L;
       
       /**
  -     * DSetEntry constructor comment.
  +     * Used to materialize DLists from the database.
        */
       public DSetEntry()
       {
           super();
       }
   
  -    public DSetEntry(PBKey pbKey)
  -    {
  -        super(pbKey);
  -    }
  -
  -    /**
  -     * DSetEntry constructor comment.
  -     * @param anId int
  -     * @param aDlistId int
  -     * @param pos int
  -     * @param anOid java.lang.String
  -     */
  -    public DSetEntry(int anId, int aDSetId, int pos, Identity anOid)
  -    {
  -        super(anId, aDSetId, pos, anOid);
  -    }
  -
       /**
        * DSetEntry constructor comment.
        * @param theDlist org.apache.ojb.server.collections.DListImpl
  @@ -61,13 +39,10 @@
        */
       public DSetEntry(DSetImpl theDSet, Object theObject)
       {
  +        super();
  +
           this.position = theDSet.size();
  -        this.dlistId = theDSet.getId();
  -        PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  -        Identity OID = new Identity(theObject,broker);
  -        broker.close();
  -        //this.serializedOID = OID.serialize();
  -        this.oid = OID;
  +        this.dlistId  = theDSet.getId();
           this.realSubject = theObject;
       }
   
  
  
  
  1.9       +29 -20    db-ojb/src/java/org/apache/ojb/odmg/collections/DListIterator.java
  
  Index: DListIterator.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DListIterator.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DListIterator.java	4 Apr 2004 23:53:39 -0000	1.8
  +++ DListIterator.java	14 Nov 2004 09:37:40 -0000	1.9
  @@ -1,6 +1,6 @@
   package org.apache.ojb.odmg.collections;
   
  -/* Copyright 2002-2004 The Apache Software Foundation
  +/* Copyright 2003-2004 The Apache Software Foundation
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -15,16 +15,17 @@
    * limitations under the License.
    */
   
  -import java.util.ListIterator;
  -
   import org.apache.ojb.odmg.TransactionImpl;
  -import org.apache.ojb.odmg.TxManagerFactory;
   import org.odmg.Transaction;
   
  +import java.util.ListIterator;
  +
   /**
    * Insert the type's description here.
    * Creation date: (09.02.2001 15:49:39)
    * @author Thomas Mahler
  + * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
  + * @version $Id$
    */
   class DListIterator implements ListIterator
   {
  @@ -37,8 +38,8 @@
        */
       public DListIterator(DListImpl list)
       {
  -        this.dlist = list;
  -        this.iter = list.getElements().listIterator();
  +        dlist = list;
  +        iter  = list.getElements().listIterator();
       }
   
       /**
  @@ -46,8 +47,8 @@
        */
       public DListIterator(DListImpl list, int index)
       {
  -        this.dlist = list;
  -        this.iter = list.getElements().listIterator(index);
  +        dlist = list;
  +        iter  = list.getElements().listIterator(index);
       }
   
       /**
  @@ -71,16 +72,19 @@
        * @exception IllegalArgumentException if some aspect of this element
        * prevents it from being added to this Collection.
        */
  -    public void add(Object o)
  +    public void add(Object obj)
       {
  -        DListEntry entry = new DListEntry(this.dlist, o);
  -        entry.setPosition(this.nextIndex() - 1);
  +        DListEntry entry = new DListEntry(dlist, obj);
  +
  +        entry.setPosition(nextIndex() - 1);
           iter.add(entry);
   
  -        TransactionImpl tx = TxManagerFactory.instance().getTransaction();
  +        TransactionImpl tx = dlist.getTxManager().getTransaction();
  +
           if (tx != null)
           {
               tx.lock(entry, Transaction.WRITE);
  +            entry.prepareForPersistency(tx.getBroker());
           }
       }
   
  @@ -118,8 +122,9 @@
        */
       public Object next()
       {
  -        currentEntry = ((DListEntry) iter.next());
  -        return currentEntry.getRealSubject();
  +        currentEntry = (DListEntry)iter.next();
  +
  +        return currentEntry.getRealSubject(dlist.getPBKey(), dlist.getTxManager());
       }
   
       /**
  @@ -148,10 +153,11 @@
        * @exception NoSuchElementException if the iteration has no previous
        * element.
        */
  -    public java.lang.Object previous()
  +    public Object previous()
       {
  -        currentEntry = ((DListEntry) iter.previous());
  -        return currentEntry.getRealSubject();
  +        currentEntry = (DListEntry)iter.previous();
  +
  +        return currentEntry.getRealSubject(dlist.getPBKey(), dlist.getTxManager());
       }
   
       /**
  @@ -187,7 +193,9 @@
       public void remove()
       {
           iter.remove();
  -        TransactionImpl tx = TxManagerFactory.instance().getTransaction();
  +
  +        TransactionImpl tx = dlist.getTxManager().getTransaction();
  +
           if (tx != null)
           {
               tx.markDelete(currentEntry);
  @@ -213,9 +221,10 @@
        * <tt>add</tt> have been called after the last call to
        * <tt>next</tt> or <tt>previous</tt>.
        */
  -    public void set(java.lang.Object o)
  +    public void set(Object o)
       {
  -        currentEntry.setRealSubject(o);
  +        throw new UnsupportedOperationException("Method is not supported");
  +        // currentEntry.setRealSubject(o);
       }
   
   }
  
  
  
  1.22      +184 -98   db-ojb/src/java/org/apache/ojb/odmg/collections/DSetImpl.java
  
  Index: DSetImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DSetImpl.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- DSetImpl.java	4 Apr 2004 23:53:39 -0000	1.21
  +++ DSetImpl.java	14 Nov 2004 09:37:40 -0000	1.22
  @@ -16,12 +16,16 @@
    */
   
   import java.io.Serializable;
  +import java.util.AbstractSet;
  +import java.util.ArrayList;
   import java.util.Iterator;
  -import java.util.Vector;
  +import java.util.List;
   
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.PersistenceBrokerAware;
   import org.apache.ojb.broker.PersistenceBrokerException;
  +import org.apache.ojb.broker.PersistenceBrokerInternal;
   import org.apache.ojb.broker.core.ValueContainer;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
  @@ -30,79 +34,95 @@
   import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
  +import org.apache.ojb.odmg.OJBTxManager;
   import org.apache.ojb.odmg.PBCapsule;
  +import org.apache.ojb.odmg.TransactionExt;
   import org.apache.ojb.odmg.TransactionImpl;
  -import org.apache.ojb.odmg.TxManagerFactory;
   import org.apache.ojb.odmg.oql.OQLQueryImpl;
   import org.odmg.DList;
   import org.odmg.DSet;
   import org.odmg.ODMGRuntimeException;
   import org.odmg.OQLQuery;
  +import org.odmg.QueryInvalidException;
   import org.odmg.Transaction;
   
   
   /**
    *
    */
  -public class DSetImpl extends java.util.AbstractSet implements org.odmg.DSet, Serializable
  +public class DSetImpl extends AbstractSet implements DSet, PersistenceBrokerAware, Serializable
   {
   	private static final long serialVersionUID = -4459673364598652639L;
       private Logger log = LoggerFactory.getLogger(DSetImpl.class);
  -    private int id = -1;
  -    private Vector elements;
  +    private Integer id;
  +    private List elements;
       private int size;
  +    private OJBTxManager txManager;
       private PBKey pbKey;
   
       /**
  -     * DSetImpl constructor comment.
  +     * An empty constructor for materializing from the database.
        */
  -    public DSetImpl()
  +    private DSetImpl()
       {
           super();
  -        elements = new Vector();
  -        this.size = 0;
  +        this.txManager = null;
  +        this.elements  = new ArrayList();
  +        this.size      = 0;
       }
   
       /**
        * DSetImpl constructor comment.
        */
  -    public DSetImpl(PBKey pbKey)
  +    public DSetImpl(OJBTxManager txManager)
       {
           super();
  -        elements = new Vector();
  -        this.size = 0;
  -        this.pbKey = pbKey;
  +        this.txManager = txManager;
  +        this.elements  = new ArrayList();
  +        this.size      = 0;
       }
   
       /**
  -     * DListImpl constructor comment.
  +     * DSetImpl constructor comment.
        */
  -    public DSetImpl(int theId, int theSize)
  +    protected DSetImpl(OJBTxManager txManager, PBKey pbKey)
       {
           super();
  -        elements = new Vector();
  -        id = theId;
  -        this.size = theSize;
  +        this.txManager = txManager;
  +        this.elements  = new ArrayList();
  +        this.size      = 0;
  +        this.pbKey     = pbKey;
       }
   
  -    private TransactionImpl getTransaction()
  +    public PBKey getPBKey()
       {
  -        return TxManagerFactory.instance().getTransaction();
  +        return pbKey;
  +    }
  +
  +    public OJBTxManager getTxManager()
  +    {
  +        return txManager;
  +    }
  +
  +    public void setTxManager(OJBTxManager txManager)
  +    {
  +        this.txManager = txManager;
       }
   
  -    /**
  -     *
  -     */
       public boolean add(Object o)
       {
  -        if (!this.contains(o))
  +        if (!contains(o))
           {
  -            this.size++;
  +            size++;
  +
               DSetEntry entry = new DSetEntry(this, o);
  +
               elements.add(entry);
  +
  +            TransactionImpl tx = txManager.getTransaction();
  +
               // if we are in a transaction: get locks !
  -            TransactionImpl tx = getTransaction();
  -            if ((tx != null) && (tx.isOpen()))
  +            if ((tx != null) && tx.isOpen())
               {
                   tx.lock(this, Transaction.WRITE);
                   tx.lock(entry, Transaction.WRITE);
  @@ -125,11 +145,12 @@
        */
       public org.odmg.DSet difference(org.odmg.DSet otherSet)
       {
  -        DSetImpl result = new DSetImpl(pbKey);
  -        Iterator iter = this.iterator();
  -        while (iter.hasNext())
  +        DSetImpl result = new DSetImpl(getTxManager(), getPBKey());
  +
  +        for (Iterator iter = this.iterator(); iter.hasNext();)
           {
               Object candidate = iter.next();
  +
               if (!otherSet.contains(candidate))
               {
                   result.add(candidate);
  @@ -146,13 +167,11 @@
        * for the predicate, otherwise false.
        * @exception	org.odmg.QueryInvalidException	The query predicate is invalid.
        */
  -    public boolean existsElement(String predicate) throws org.odmg.QueryInvalidException
  +    public boolean existsElement(String predicate) throws QueryInvalidException
       {
  -        DList results = (DList) this.query(predicate);
  -        if (results == null || results.size() == 0)
  -            return false;
  -        else
  -            return true;
  +        DList results = (DList)query(predicate);
  +
  +        return (results != null) && (results.size() > 0);
       }
   
       /**
  @@ -160,17 +179,15 @@
        */
       protected int generateNewId()
       {
  -        PBCapsule capsule = new PBCapsule(null, TxManagerFactory.instance().getTransaction());
  +        PBCapsule capsule = new PBCapsule(null, txManager.getTransaction());
  +
           try
           {
  -        	ClassDescriptor cld = capsule.getBroker().getClassDescriptor(this.getClass());
  +        	ClassDescriptor cld = capsule.getBroker().getClassDescriptor(getClass());
           	FieldDescriptor fld = cld.getFieldDescriptorByName("id");
  +            Integer         val = (Integer)capsule.getBroker().serviceSequenceManager().getUniqueValue(fld);
   
  -            Integer val = (Integer) capsule.getBroker().serviceSequenceManager().getUniqueValue(fld);
  -
  -            int result = val.intValue();
  -
  -            return result;
  +            return val.intValue();
           }
           catch (Exception e)
           {
  @@ -184,27 +201,13 @@
       }
   
   
  -    /**
  -     * Insert the method's description here.
  -     * Creation date: (26.02.2001 13:29:42)
  -     * @return java.util.Vector
  -     */
  -    public java.util.Vector getElements()
  +    public List getElements()
       {
           return elements;
       }
   
  -    /**
  -     * Insert the method's description here.
  -     * Creation date: (26.02.2001 13:50:57)
  -     * @return int
  -     */
  -    public int getId()
  -    {
  -    	if (id == -1)
  -    	{
  -    		id = generateNewId();
  -    	}
  +    public Integer getId()
  +    {
           return id;
       }
   
  @@ -217,13 +220,14 @@
        */
       public org.odmg.DSet intersection(org.odmg.DSet otherSet)
       {
  -        DSet union = this.union(otherSet);
  -        DSetImpl result = new DSetImpl(pbKey);
  -        Iterator iter = union.iterator();
  -        while (iter.hasNext())
  +        DSet     union  = union(otherSet);
  +        DSetImpl result = new DSetImpl(getTxManager(), getPBKey());
  +
  +        for (Iterator iter = union.iterator(); iter.hasNext();)
           {
               Object candidate = iter.next();
  -            if (this.contains(candidate) && otherSet.contains(candidate))
  +
  +            if (contains(candidate) && otherSet.contains(candidate))
               {
                   result.add(candidate);
               }
  @@ -239,7 +243,7 @@
        *
        * @return an <tt>Iterator</tt> over the elements in this collection
        */
  -    public java.util.Iterator iterator()
  +    public Iterator iterator()
       {
           return new DSetIterator(this);
       }
  @@ -253,7 +257,7 @@
        */
       public boolean properSubsetOf(org.odmg.DSet otherSet)
       {
  -        return (this.size() > 0 && this.size() < otherSet.size() && this.subsetOf(otherSet));
  +        return (size() > 0) && (size() < otherSet.size()) && subsetOf(otherSet);
       }
   
       /**
  @@ -265,7 +269,7 @@
        */
       public boolean properSupersetOf(org.odmg.DSet otherSet)
       {
  -        return (otherSet.size() > 0 && otherSet.size() < this.size() && this.supersetOf(otherSet));
  +        return (otherSet.size() > 0) && (otherSet.size() < size()) && supersetOf(otherSet);
       }
   
       /**
  @@ -278,27 +282,33 @@
       public org.odmg.DCollection query(String predicate) throws org.odmg.QueryInvalidException
       {
           // 1.build complete OQL statement
  -        String oql = "select all from java.lang.Object where " + predicate;
  -        OQLQuery predicateQuery = new OQLQueryImpl(pbKey);
  -
  -        Transaction tx = getTransaction();
  -        PBCapsule capsule = new PBCapsule(pbKey, tx);
  -        PersistenceBroker broker = capsule.getBroker();
  +        String            oql            = "select all from java.lang.Object where " + predicate;
  +        OQLQuery          predicateQuery = new OQLQueryImpl(pbKey, txManager, getClass());
  +        TransactionExt    tx             = txManager.getTransaction();
  +        PBCapsule         capsule        = new PBCapsule(pbKey, tx);
  +        PersistenceBroker broker         = capsule.getBroker();
   
           try
           {
               predicateQuery.create(oql);
  -            Query pQ = ((OQLQueryImpl) predicateQuery).getQuery();
  -            Criteria pCrit = pQ.getCriteria();
   
  -            Criteria allElementsCriteria = this.getPkCriteriaForAllElements(broker);
  +            Query    pQ                  = ((OQLQueryImpl)predicateQuery).getQuery();
  +            Criteria pCrit               = pQ.getCriteria();
  +            Criteria allElementsCriteria = getPkCriteriaForAllElements(broker);
  +
               // join selection of elements with predicate criteria:
               pCrit.addAndCriteria(allElementsCriteria);
  -            Class clazz = this.getElementsExtentClass(broker);
  -            Query q = new QueryByCriteria(clazz, pCrit);
  -            if (log.isDebugEnabled()) log.debug(q.toString());
  +
  +            Class clazz = getElementsExtentClass(broker);
  +            Query q     = new QueryByCriteria(clazz, pCrit);
  +
  +            if (log.isDebugEnabled())
  +            {
  +                log.debug(q.toString());
  +            }
  +
               // 2. perfom query
  -            return (DSetImpl) broker.getCollectionByQuery(DSetImpl.class, q);
  +            return (DSetImpl)broker.getCollectionByQuery(DSetImpl.class, q);
           }
           catch (PersistenceBrokerException e)
           {
  @@ -315,26 +325,31 @@
           try
           {
               Criteria crit = null;
  +
               for (int i = 0; i < elements.size(); i++)
               {
  -                DListEntry entry = (DListEntry) elements.get(i);
  -                Object obj = entry.getRealSubject();
  -                ClassDescriptor cld = broker.getClassDescriptor(obj.getClass());
  -
  +                DListEntry        entry    = (DListEntry)elements.get(i);
  +                Object            obj      = entry.getRealSubject(getPBKey(), getTxManager());
  +                ClassDescriptor   cld      = broker.getClassDescriptor(obj.getClass());
                   FieldDescriptor[] pkFields = cld.getPkFields();
  -                ValueContainer[] pkValues = broker.serviceBrokerHelper().getKeyValues(cld, obj);
  +                ValueContainer[]  pkValues = broker.serviceBrokerHelper().getKeyValues(cld, obj);
  +                Criteria          criteria = new Criteria();
   
  -                Criteria criteria = new Criteria();
                   for (int j = 0; j < pkFields.length; j++)
                   {
                       FieldDescriptor fld = pkFields[j];
  +
                       criteria.addEqualTo(fld.getPersistentField().getName(), pkValues[j].getValue());
                   }
   
                   if (crit == null)
  +                {
                       crit = criteria;
  +                }
                   else
  +                {
                       crit.addOrCriteria(criteria);
  +                }
               }
               return crit;
           }
  @@ -348,10 +363,10 @@
       private Class getElementsExtentClass(PersistenceBroker broker) throws PersistenceBrokerException
       {
           // we ll have to compute the most general extent class here !!!
  -        DListEntry entry = (DListEntry) elements.get(0);
  -        Class elementsClass = entry.getRealSubject().getClass();
  -        Class extentClass = broker.getTopLevelClass(elementsClass);
  -        return extentClass;
  +        DListEntry entry         = (DListEntry)elements.get(0);
  +        Class      elementsClass = entry.getRealSubject(getPBKey(), getTxManager()).getClass();
  +
  +        return broker.getTopLevelClass(elementsClass);
       }
   
   
  @@ -362,9 +377,9 @@
        * @return	An iterator used to iterate over the elements that evaluated true for the predicate.
        * @exception	org.odmg.QueryInvalidException	The query predicate is invalid.
        */
  -    public java.util.Iterator select(String predicate) throws org.odmg.QueryInvalidException
  +    public Iterator select(String predicate) throws org.odmg.QueryInvalidException
       {
  -        return this.query(predicate).iterator();
  +        return query(predicate).iterator();
       }
   
       /**
  @@ -377,14 +392,14 @@
        */
       public Object selectElement(String predicate) throws org.odmg.QueryInvalidException
       {
  -        return ((DList) this.query(predicate)).get(0);
  +        return ((DList)query(predicate)).get(0);
       }
   
       /**
        * Sets the elements.
        * @param elements The elements to set
        */
  -    public void setElements(Vector elements)
  +    public void setElements(List elements)
       {
           this.elements = elements;
       }
  @@ -393,7 +408,7 @@
        * Sets the id.
        * @param id The id to set
        */
  -    public void setId(int id)
  +    public void setId(Integer id)
       {
           this.id = id;
       }
  @@ -429,7 +444,7 @@
        */
       public boolean supersetOf(org.odmg.DSet otherSet)
       {
  -        return this.containsAll(otherSet);
  +        return containsAll(otherSet);
       }
   
       /**
  @@ -440,7 +455,8 @@
        */
       public org.odmg.DSet union(org.odmg.DSet otherSet)
       {
  -        DSetImpl result = new DSetImpl(pbKey);
  +        DSetImpl result = new DSetImpl(getTxManager(), getPBKey());
  +
           result.addAll(this);
           result.addAll(otherSet);
           return result;
  @@ -462,5 +478,75 @@
       public void setSize(int size)
       {
           this.size = size;
  +    }
  +
  +
  +    //***************************************************************
  +    // PersistenceBrokerAware interface
  +    //***************************************************************
  +
  +    /**
  +     * prepare itself for persistence. Each DSet entry generates an
  +     * {@link org.apache.ojb.broker.Identity} for the wrapped persistent
  +     * object.
  +     */
  +    public void beforeInsert(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +        for (Iterator it = elements.iterator(); it.hasNext();)
  +        {
  +            ((DSetEntry)it.next()).prepareForPersistency(broker);
  +        }
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void beforeUpdate(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void beforeDelete(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterUpdate(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterInsert(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterDelete(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +    }
  +
  +    /**
  +     * noop
  +     */
  +    public void afterLookup(PersistenceBroker broker) throws PersistenceBrokerException
  +    {
  +        if (pbKey == null)
  +        {
  +            pbKey = broker.getPBKey();
  +        }
  +        if (txManager == null)
  +        {
  +            // TODO: this is something of a is a hack, but currently the only way to get the current
  +            //       tx manager to a collection retrieved via Database#lookup or via a PB read
  +            txManager = (OJBTxManager)((PersistenceBrokerInternal)broker).getConfiguration().getComponentContainer().getSingletonInstance(OJBTxManager.class);
  +        }
       }
   }
  
  
  
  1.8       +27 -27    db-ojb/src/java/org/apache/ojb/odmg/collections/DBagImpl.java
  
  Index: DBagImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DBagImpl.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DBagImpl.java	4 Apr 2004 23:53:39 -0000	1.7
  +++ DBagImpl.java	14 Nov 2004 09:37:40 -0000	1.8
  @@ -15,7 +15,7 @@
    * limitations under the License.
    */
   
  -import org.apache.ojb.broker.PBKey;
  +import org.apache.ojb.odmg.OJBTxManager;
   import org.odmg.DBag;
   
   import java.util.Iterator;
  @@ -25,31 +25,24 @@
    * Creation date: (25.02.2001 12:53:12)
    * @author Thomas Mahler
    */
  -public class DBagImpl extends DListImpl implements org.odmg.DBag
  +public class DBagImpl extends DListImpl implements DBag
   {
       private static final long serialVersionUID = -4937635522392824190L;
  -    
  -    public DBagImpl()
  -    {
  -        super();
  -    }
  -
  + 
       /**
  -     * DBagImpl constructor comment.
  +     * Empty constructor for creating a bag object from the database.
        */
  -    public DBagImpl(PBKey key)
  +    public DBagImpl()
       {
  -        super(key);
  +        super();
       }
   
       /**
  -     * DBagImpl constructor comment.
  -     * @param theId int
  -     * @param theSize int
  +     * Internally-used constructor.
        */
  -    public DBagImpl(int theId, int theSize)
  +    public DBagImpl(OJBTxManager txManager)
       {
  -        super(theId, theSize);
  +        super(txManager);
       }
   
       /**
  @@ -64,11 +57,14 @@
        */
       public DBag difference(DBag otherBag)
       {
  -        DBagImpl result = new DBagImpl(pbKey);
  -        Iterator iter = this.iterator();
  -        while (iter.hasNext())
  +        DBagImpl result = new DBagImpl();
  +
  +        result.setTxManager(getTxManager());
  +        result.setPBKey(getPBKey());
  +        for (Iterator iter = this.iterator(); iter.hasNext();)
           {
               Object candidate = iter.next();
  +
               if (!otherBag.contains(candidate))
               {
                   result.add(candidate);
  @@ -89,12 +85,15 @@
        */
       public DBag intersection(org.odmg.DBag otherBag)
       {
  -        DBagImpl result = new DBagImpl(pbKey);
  -        java.util.Iterator iter = otherBag.iterator();
  -        while (iter.hasNext())
  +        DBagImpl result = new DBagImpl();
  +
  +        result.setTxManager(getTxManager());
  +        result.setPBKey(getPBKey());
  +        for (Iterator iter = otherBag.iterator(); iter.hasNext();)
           {
               Object candidate = iter.next();
  -            if (this.contains(candidate))
  +
  +            if (contains(candidate))
               {
                   result.add(candidate);
               }
  @@ -111,9 +110,10 @@
       public int occurrences(Object obj)
       {
           int count = 0;
  -        for (int i = 0; i < this.size(); i++)
  +
  +        for (int i = 0; i < size(); i++)
           {
  -            if ((obj == null) ? this.get(i) == null : this.get(i).equals(obj))
  +            if ((obj == null) ? get(i) == null : get(i).equals(obj))
               {
                   count++;
               }
  @@ -131,9 +131,9 @@
        * @return A <code>DBag</code> instance that contains the union of this object
        * and <code>otherBag</code>.
        */
  -    public org.odmg.DBag union(DBag otherBag)
  +    public DBag union(DBag otherBag)
       {
  -        return (DBagImpl) concat((DBagImpl) otherBag);
  +        return (DBagImpl)concat((DBagImpl)otherBag);
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message