You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2006/01/17 01:10:53 UTC

svn commit: r369611 - in /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker: TransientObjectException.java core/PersistenceBrokerImpl.java core/QueryReferenceBroker.java

Author: arminw
Date: Mon Jan 16 16:10:46 2006
New Revision: 369611

URL: http://svn.apache.org/viewcvs?rev=369611&view=rev
Log:
fix problem with transient Identity objects

Added:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/TransientObjectException.java
Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java

Added: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/TransientObjectException.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/TransientObjectException.java?rev=369611&view=auto
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/TransientObjectException.java (added)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/TransientObjectException.java Mon Jan 16 16:10:46 2006
@@ -0,0 +1,45 @@
+package org.apache.ojb.broker;
+
+/* Copyright 2002-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.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.lang.Object;
+
+/**
+ * This exception signals a problem with a transient object.
+ *
+ * @version $Id: $
+ */
+public class TransientObjectException extends OJBException
+{
+    public TransientObjectException()
+    {
+    }
+
+    public TransientObjectException(String msg)
+    {
+        super(msg);
+    }
+
+    public TransientObjectException(Throwable cause)
+    {
+        super(cause);
+    }
+
+    public TransientObjectException(String msg, Throwable cause)
+    {
+        super(msg, cause);
+    }
+}

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java?rev=369611&r1=369610&r2=369611&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java Mon Jan 16 16:10:46 2006
@@ -36,6 +36,7 @@
 import org.apache.ojb.broker.TransactionAbortedException;
 import org.apache.ojb.broker.TransactionInProgressException;
 import org.apache.ojb.broker.TransactionNotInProgressException;
+import org.apache.ojb.broker.TransientObjectException;
 import org.apache.ojb.broker.accesslayer.ChainingIterator;
 import org.apache.ojb.broker.accesslayer.ConnectionManagerFactory;
 import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
@@ -642,29 +643,40 @@
                 {
                     oid = serviceIdentity().buildIdentity(oid);
                 }
-                query = referencesBroker.getPKQuery((Identity) oid);
-            }
-
-            if (!cld.isInterface())
-            {
-                this.dbAccess.executeDelete(query, cld);
+                try
+                {
+                    query = referencesBroker.getPKQuery((Identity) oid);
+                }
+                catch(TransientObjectException e)
+                {
+                    query = null;
+                    logger.error("Can't delete transient objects from datastore", e);
+                }
             }
 
-            // if class is an extent, we have to delete all extent classes too
-            String lastUsedTable = cld.getFullTableName();
-            if (cld.isExtent())
+            if(query != null)
             {
-                Iterator extents = getDescriptorRepository().getAllConcreteSubclassDescriptors(cld).iterator();
+                if (!cld.isInterface())
+                {
+                    this.dbAccess.executeDelete(query, cld);
+                }
 
-                while (extents.hasNext())
+                // if class is an extent, we have to delete all extent classes too
+                String lastUsedTable = cld.getFullTableName();
+                if (cld.isExtent())
                 {
-                    ClassDescriptor extCld = (ClassDescriptor) extents.next();
+                    Iterator extents = getDescriptorRepository().getAllConcreteSubclassDescriptors(cld).iterator();
 
-                    // read same table only once
-                    if (!extCld.getFullTableName().equals(lastUsedTable))
+                    while (extents.hasNext())
                     {
-                        lastUsedTable = extCld.getFullTableName();
-                        this.dbAccess.executeDelete(query, extCld);
+                        ClassDescriptor extCld = (ClassDescriptor) extents.next();
+
+                        // read same table only once
+                        if (!extCld.getFullTableName().equals(lastUsedTable))
+                        {
+                            lastUsedTable = extCld.getFullTableName();
+                            this.dbAccess.executeDelete(query, extCld);
+                        }
                     }
                 }
             }
@@ -1733,19 +1745,31 @@
         {
             // example obj may be an entity or an Identity
             Object obj = query.getExampleObject();
+            Identity oid;
             if (obj instanceof Identity)
             {
-                Identity oid = (Identity) obj;
-                result = getObjectByIdentity(oid);
+                oid = (Identity) obj;
             }
             else
             {
-                // TODO: This workaround doesn't allow 'null' for PK fields
-                if (!serviceBrokerHelper().hasNullPKField(getClassDescriptor(obj.getClass()), obj))
-                {
-                    Identity oid = serviceIdentity().buildIdentity(obj);
-                    result = getObjectByIdentity(oid);
-                }
+//                if (!serviceBrokerHelper().hasNullPKField(getClassDescriptor(obj.getClass()), obj))
+//                {
+//                    oid = serviceIdentity().buildIdentity(obj);
+//                }
+                // TODO: Check this! The above check is no longer needed, because "new objects" will automatic
+                // have transient Identity objects and for these the result is always null
+                oid = serviceIdentity().buildIdentity(obj);
+            }
+            if(oid.isTransient())
+            {
+                if(logger.isEnabledFor(Logger.INFO)) logger.info(
+                        "The object to query was detected as transient, will only lookup the cache for object instance. Identity="
+                        + oid + ", the query object was " + query);
+                result = this.objectCache.lookup(oid);
+            }
+            else
+            {
+                result = getObjectByIdentity(oid);
             }
         }
         else

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java?rev=369611&r1=369610&r2=369611&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java Mon Jan 16 16:10:46 2006
@@ -27,6 +27,7 @@
 import org.apache.ojb.broker.ManageableCollection;
 import org.apache.ojb.broker.PBLifeCycleEvent;
 import org.apache.ojb.broker.PersistenceBrokerException;
+import org.apache.ojb.broker.TransientObjectException;
 import org.apache.ojb.broker.accesslayer.OJBIterator;
 import org.apache.ojb.broker.accesslayer.PagingIterator;
 import org.apache.ojb.broker.accesslayer.PlainPrefetcher;
@@ -914,9 +915,16 @@
      *
      * @param oid the Identity of the Object to retrieve
      * @return The resulting query
+     * @throws TransientObjectException If the specified {@link org.apache.ojb.broker.Identity}
+     * is <em>transient</em> this exception will be thrown, because it's not possible to build
+     * a valid query.
      */
-    public Query getPKQuery(Identity oid)
+    public Query getPKQuery(Identity oid) throws TransientObjectException
     {
+        if(oid.isTransient())
+        {
+            throw new TransientObjectException("Not allowed to build PK Query for a transient object");
+        }
         Object[] values = oid.getPrimaryKeyValues();
         ClassDescriptor cld = pb.getClassDescriptor(oid.getObjectsTopLevelClass());
         FieldDescriptor[] fields = cld.getPkFields();



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