openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From curti...@apache.org
Subject svn commit: r986963 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBroker.java
Date Wed, 18 Aug 2010 21:52:59 GMT
Author: curtisr7
Date: Wed Aug 18 21:52:59 2010
New Revision: 986963

URL: http://svn.apache.org/viewvc?rev=986963&view=rev
Log:
OPENJPA-1769: Fix Broker.getObjectId(..) to work on detached Entities with no DetachedStateManager.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBroker.java
  (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=986963&r1=986962&r2=986963&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java Wed
Aug 18 21:52:59 2010
@@ -4423,9 +4423,18 @@ public class BrokerImpl
 
     public Object getObjectId(Object obj) {
         assertOpen();
-        if (ImplHelper.isManageable(obj))
-            return (ImplHelper.toPersistenceCapable(obj, _conf))
-                .pcFetchObjectId();
+        if (ImplHelper.isManageable(obj)) {
+            PersistenceCapable pc = ImplHelper.toPersistenceCapable(obj, _conf);
+            if (pc != null) {
+                if (pc.pcGetStateManager() == null) {
+                    MetaDataRepository repo = _conf.getMetaDataRepositoryInstance();
+                    // If the statemanager is null the call to pcFetchObjectId always returns
null. Create a new object
+                    // id.
+                    return ApplicationIds.create(pc, repo.getMetaData(pc.getClass(), null,
true));
+                }
+                return pc.pcFetchObjectId();
+            }
+        }
         return null;
     }
 

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBroker.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBroker.java?rev=986963&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBroker.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBroker.java
Wed Aug 18 21:52:59 2010
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.openjpa.kernel;
+
+import org.apache.openjpa.jdbc.kernel.A;
+import org.apache.openjpa.persistence.EntityManagerImpl;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestBroker extends SingleEMFTestCase {
+    public void setUp() {
+        super.setUp(A.class, CLEAR_TABLES);
+    }
+
+    /**
+     * This test asserts that the ObjectId obtained from the Broker is the same for the following
cases: [ An Entity
+     * that exists in the current persistence context, a detached Entity with a DetachedStateManager,
and a detached
+     * Entity with no DetachedStateManager]
+     */
+    public void testGetObjectId() {
+        EntityManagerImpl em = (EntityManagerImpl) emf.createEntityManager();
+        Broker broker = em.getBroker();
+
+        em.getTransaction().begin();
+        A a = new A();
+        em.persist(a);
+        Object oidInPersistenceContext = broker.getObjectId(a);
+        em.getTransaction().commit();
+        em.clear();
+        Object oidDetached = broker.getObjectId(a);
+        em.close();
+
+        em = (EntityManagerImpl) emf.createEntityManager();
+        broker = em.getBroker();
+        A a2 = new A();
+        a2.setId(a.getId());
+
+        Object oidDetachedNoSm = broker.getObjectId(a2);
+
+        assertTrue(oidInPersistenceContext.equals(oidDetached));
+        assertTrue(oidDetached.equals(oidDetachedNoSm));
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/kernel/TestBroker.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message