openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r614564 - in /openjpa/branches/1.0.x: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/
Date Wed, 23 Jan 2008 15:17:46 GMT
Author: mikedd
Date: Wed Jan 23 07:17:42 2008
New Revision: 614564

URL: http://svn.apache.org/viewvc?rev=614564&view=rev
Log:
OPENJPA-245

Added:
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Person.java
  (with props)
Modified:
    openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java
    openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/VersionAttachStrategy.java
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerMerge.java

Modified: openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java?rev=614564&r1=614563&r2=614564&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java
(original)
+++ openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AttachManager.java
Wed Jan 23 07:17:42 2008
@@ -98,7 +98,11 @@
 
         CallbackException excep = null;
         try {
-            return attach(pc, null, null, null, true);
+            PersistenceCapable into = findFromDatabase(pc);
+            OpenJPAStateManager owner = (into == null) ? null
+                    : (OpenJPAStateManager) into.pcGetStateManager();
+            return attach(pc, into, owner, null, true);
+
         } catch (CallbackException ce) {
             excep = ce;
             return null; // won't be reached as the exceps will be rethrown
@@ -335,4 +339,30 @@
                 Exceptions.toString(obj))).setFailedObject (obj);
 		return sm;
 	}
+
+    /**
+     * Find a PersistenceCapable instance of an Object if it exists in the 
+     * database. If the object is null or can't be found in the database.  
+     *  
+     * @param pc An object which will be attached into the current context. The 
+     * object may or may not correspond to a row in the database. 
+     * 
+     * @return If the object is null or can't be found in the database this 
+     * method returns null. Otherwise a PersistenceCapable representation of the
+     * object is returned.
+     */
+    protected PersistenceCapable findFromDatabase(Object pc) {
+        PersistenceCapable rval = null;
+
+        if (pc != null) {
+            Object oid = _broker.newObjectId(pc.getClass(),
+                    getDetachedObjectId(pc));
+
+            if (oid != null) {
+                rval = ImplHelper.toPersistenceCapable(_broker.find(oid, true,
+                        null), getBroker().getConfiguration());
+            }
+        }
+        return rval;
+    }
 }

Modified: openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/VersionAttachStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/VersionAttachStrategy.java?rev=614564&r1=614563&r2=614564&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/VersionAttachStrategy.java
(original)
+++ openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/VersionAttachStrategy.java
Wed Jan 23 07:17:42 2008
@@ -73,7 +73,7 @@
             meta.getRepository().getConfiguration());
 
         boolean embedded = ownerMeta != null && ownerMeta.isEmbeddedPC();
-        boolean isNew = !broker.isDetached(pc);
+        boolean isNew = !broker.isDetached(pc) && into == null;
         Object version = null;
         StateManagerImpl sm;
 

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Person.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Person.java?rev=614564&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Person.java
(added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Person.java
Wed Jan 23 07:17:42 2008
@@ -0,0 +1,56 @@
+/*
+ * 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.persistence.simple;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.Version;
+
+@Entity
+public class Person {
+    private int id;
+    private String forename;
+    private String surname;
+
+    @Id
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getForename() {
+        return forename;
+    }
+
+    public void setForename(String forename) {
+        this.forename = forename;
+    }
+
+    public String getSurname() {
+        return surname;
+    }
+
+    public void setSurname(String surname) {
+        this.surname = surname;
+    }
+}

Propchange: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/Person.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerMerge.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerMerge.java?rev=614564&r1=614563&r2=614564&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerMerge.java
(original)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestEntityManagerMerge.java
Wed Jan 23 07:17:42 2008
@@ -18,6 +18,8 @@
  */
 package org.apache.openjpa.persistence.simple;
 
+import javax.persistence.EntityManager;
+
 import junit.textui.TestRunner;
 import org.apache.openjpa.persistence.test.SingleEMTestCase;
 
@@ -30,7 +32,7 @@
     extends SingleEMTestCase {
 
     public void setUp() {
-        setUp(AllFieldTypes.class);
+        setUp(AllFieldTypes.class, Person.class);
     }
 
     public void testMerge() {
@@ -68,6 +70,42 @@
         // Rollback
         rollback();
   
+    }
+    
+    /**
+     * This test verifies that persisting a new entity which matches an existing 
+     * row in the database succeeds. 
+     */
+    public void testMergeExistingEntity() {
+        Person p = new Person();
+        p.setId(102);
+
+        EntityManager em = emf.createEntityManager();
+
+        em.getTransaction().begin();
+        em.persist(p);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        p = new Person();
+        p.setId(102);
+        p.setForename("Jane");
+
+        em.getTransaction().begin();
+        em.merge(p);
+        em.getTransaction().commit();
+
+        em.close();
+
+        em = emf.createEntityManager();
+        p = (Person) em.createQuery("Select p from Person p where p.id = 102")
+                .getSingleResult();
+
+        assertNotNull(p);
+        assertEquals("Jane", p.getForename());
+        
+        em.close();
     }
     
     public static void main(String[] args) {



Mime
View raw message