openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r817399 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/tes...
Date Mon, 21 Sep 2009 20:21:51 GMT
Author: faywang
Date: Mon Sep 21 20:21:50 2009
New Revision: 817399

URL: http://svn.apache.org/viewvc?rev=817399&view=rev
Log:
OPENJPA-1253: support non-default uni-directional OneToMany mapping using foreign key strategy.

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationToManyInverseKeyFieldStrategy.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationToManyInverseKeyFieldStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationToManyInverseKeyFieldStrategy.java?rev=817399&r1=817398&r2=817399&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationToManyInverseKeyFieldStrategy.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationToManyInverseKeyFieldStrategy.java
Mon Sep 21 20:21:50 2009
@@ -42,6 +42,7 @@
 import org.apache.openjpa.kernel.StoreContext;
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.meta.FieldMetaData;
 import org.apache.openjpa.meta.JavaTypes;
 import org.apache.openjpa.util.ChangeTracker;
 import org.apache.openjpa.util.InternalException;
@@ -63,6 +64,7 @@
 
     private boolean _orderInsert = false;
     private boolean _orderUpdate = false;
+    private boolean _uni1MFK = false;
 
     protected ClassMapping[] getIndependentElementMappings(boolean traverse) {
         return field.getElementMapping().getIndependentTypeMappings();
@@ -153,10 +155,20 @@
             	field.setOrderColumnIO(finfo.getColumnIO());
             }
             return;
+        } else { 
+            if (field.getValueInfo().getColumns().size() > 0 && 
+                field.getAccessType() == FieldMetaData.ONE_TO_MANY) {
+                _uni1MFK = true;
+            }
         }
 
         // map inverse foreign key in related table
         ForeignKey fk = vinfo.getInverseTypeJoin(elem, field.getName(), adapt);
+        if (_uni1MFK) {
+            Column[] locals = fk.getColumns();
+            for (int i = 0; i < locals.length; i++)
+                locals[i].setUni1MFK(true);
+        }
         elem.setForeignKey(fk);
         elem.setColumnIO(vinfo.getColumnIO());
         elem.setColumns(elem.getTypeMapping().getPrimaryKeyColumns());

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java?rev=817399&r1=817398&r2=817399&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java Mon
Sep 21 20:21:50 2009
@@ -75,6 +75,7 @@
     private VersionStrategy _versionStrategy = null;
     private String _comment = null;
     private boolean _XML = false;
+    private boolean _isUni1MFK = false;
     
     /**
      * Default constructor.
@@ -733,6 +734,8 @@
             _flags = from._flags;
         if (!isXML())
             setXML(from.isXML());
+        if (!isUni1MFK())
+            setUni1MFK(from.isUni1MFK());
     }
     
     /**
@@ -792,4 +795,26 @@
     public void setImplicitRelation(boolean flag) {
     	_implicitRelation |= flag;
     }
+    
+    /**
+     * Sets a marker to indicate that this instance represents a uni-directional 
+     * one to many relation using the foreign key strategy. This non-default 
+     * mapping of uni-directional one-to-many is supported in JPA 2.0.  
+     * 
+     * @since 2.0
+     */
+    public boolean isUni1MFK() {
+        return _isUni1MFK;
+    }
+    
+    /** 
+     *  Affirms if this instance represents a uni-directional one to many relation
+     *  using the foreign key strategy. This non-default mapping of uni-directional 
+     *  one-to-many is supported in JPA 2.0.  
+     *
+     * @since 2.0
+     */
+    public void setUni1MFK(boolean isUni1MFK) {
+        _isUni1MFK = isUni1MFK;
+    }
 }

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java?rev=817399&r1=817398&r2=817399&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java Mon
Sep 21 20:21:50 2009
@@ -361,8 +361,8 @@
      * is a default value or was not set and the column is not a primary key.
      */
     boolean allowsUpdate(Column col, Object old, Object cur) {
-    	return !col.isPrimaryKey() && col.isImplicitRelation()
-    	   && (isDefaultValue(old));
+    	return ((!col.isPrimaryKey() && col.isImplicitRelation()) ||
+    	   col.isUni1MFK()) && (isDefaultValue(old));
     }
     
     boolean isDefaultValue(Object val) {

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java?rev=817399&r1=817398&r2=817399&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestSpecCompatibilityOptions.java
Mon Sep 21 20:21:50 2009
@@ -221,22 +221,34 @@
         cs.add(c1);
         em.persist(c1);
         em.getTransaction().commit();
+        
+        // update by removing a c and then add this c to a new u
+        em.getTransaction().begin();
+        EntityC_U1MFK c2 = cs.remove(0);
+        
+        Uni_1ToM_FK u2 = new Uni_1ToM_FK();
+        u2.setName("uni1mfk2");
+        List<EntityC_U1MFK> cs2 = new ArrayList<EntityC_U1MFK>();
+        cs2.add(c2);
+        u2.setEntityCs(cs2);
+        em.persist(u2);
+        em.getTransaction().commit();
         em.clear();
         
         //query
-        Query q = em.createQuery("SELECT u FROM Uni_1ToM_FK u");
+        Query q = em.createQuery("SELECT u FROM Uni_1ToM_FK u where u.name = 'newName'");
         Uni_1ToM_FK u1 = (Uni_1ToM_FK)q.getSingleResult();
         assertEquals(u, u1);
         em.clear();
 
         //find
         long id = u1.getId();
-        Uni_1ToM_FK u2 = em.find(Uni_1ToM_FK.class, id);
-        assertEquals(u, u2);
+        Uni_1ToM_FK findU1 = em.find(Uni_1ToM_FK.class, id);
+        assertEquals(findU1, u1);
         
         //remove
         em.getTransaction().begin();
-        em.remove(u2);
+        em.remove(findU1);
         em.getTransaction().commit();
         em.clear();
     }



Mime
View raw message