openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r741595 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-kernel/src/main/java/org/apache/openjpa/meta/ openjpa-persistence/src/main/java/org...
Date Fri, 06 Feb 2009 15:34:54 GMT
Author: faywang
Date: Fri Feb  6 15:34:53 2009
New Revision: 741595

URL: http://svn.apache.org/viewvc?rev=741595&view=rev
Log:
OPENJPA-871: MappedById support

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/MetaDataTag.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java?rev=741595&r1=741594&r2=741595&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java
Fri Feb  6 15:34:53 2009
@@ -139,19 +139,12 @@
         if (val.getTypeCode() != JavaTypes.PC || val.isEmbeddedPC())
             throw new MetaDataException(_loc.get("not-relation", val));
         FieldMapping mapped = field.getMappedByMapping();
-        String keyName = null;
-        _kio = new ColumnIO();
-        if (mapped != null) { // map to the owner table
-            keyName = field.getName() + "_KEY";
-            _kcols = HandlerStrategies.map(key, keyName, _kio, adapt);
+        
+        if (mapped != null) // map to the owner table
             handleMappedBy(adapt);
-        } else { 
+        else { 
             // map to a separate table
             field.mapJoin(adapt, true);
-            DBDictionary dict = field.getMappingRepository().getDBDictionary();
-            keyName = dict.getValidColumnName("key", field.getTable()); 
-            _kcols = HandlerStrategies.map(key, keyName, _kio, adapt);
-
             if (val.getTypeMapping().isMapped()) {
                 ValueMappingInfo vinfo = val.getValueInfo();
                 ForeignKey fk = vinfo.getTypeJoin(val, "value", false, adapt);
@@ -162,6 +155,10 @@
 
             val.mapConstraints("value", adapt);
         }
+        _kio = new ColumnIO();
+        DBDictionary dict = field.getMappingRepository().getDBDictionary();
+        _kcols = HandlerStrategies.map(key, 
+            dict.getValidColumnName("key", field.getTable()), _kio, adapt);
 
         field.mapPrimaryKey(adapt);
     }

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java?rev=741595&r1=741594&r2=741595&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
Fri Feb  6 15:34:53 2009
@@ -19,7 +19,9 @@
 package org.apache.openjpa.jdbc.meta.strats;
 
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -35,6 +37,7 @@
 import org.apache.openjpa.jdbc.meta.Joinable;
 import org.apache.openjpa.jdbc.meta.MappingInfo;
 import org.apache.openjpa.jdbc.meta.ValueMapping;
+import org.apache.openjpa.jdbc.meta.ValueMappingImpl;
 import org.apache.openjpa.jdbc.meta.ValueMappingInfo;
 import org.apache.openjpa.jdbc.schema.Column;
 import org.apache.openjpa.jdbc.schema.ColumnIO;
@@ -151,6 +154,9 @@
 
         field.mapJoin(adapt, false);
         if (field.getTypeMapping().isMapped()) {
+            if (field.getMappedByIdValue() != null) 
+                setMappedByIdColumns();            
+             
             ForeignKey fk = vinfo.getTypeJoin(field, field.getName(), true,
                 adapt);
             field.setForeignKey(fk);
@@ -178,6 +184,44 @@
     }
 
     /**
+     * When there is MappedById annotation, the owner of the one-to-one/
+     * many-to-one relationship will use its primary key to represent 
+     * foreign key relation. No need to create a separate foreign key
+     * column. 
+     */
+    private void setMappedByIdColumns() {
+        ClassMetaData owner = field.getDefiningMetaData();
+        FieldMetaData[] pks = owner.getPrimaryKeyFields();
+        for (int i = 0; i < pks.length; i++) {
+            FieldMapping fm = (FieldMapping) pks[i];
+            ValueMappingImpl val = (ValueMappingImpl) field.getValue();
+            ValueMappingInfo info = val.getValueInfo();
+            info.setColumns(getMappedByIdColumns(fm));
+        }
+    }
+
+    private List getMappedByIdColumns(FieldMapping pk) {
+        ClassMetaData embeddedId = ((ValueMappingImpl)pk.getValue()).getEmbeddedMetaData();
+        Column[] pkCols = null;
+        if (embeddedId != null) {
+            FieldMetaData[] fmds = embeddedId.getFields();
+            for (int i = 0; i < fmds.length; i++) {
+                if (fmds[i].getName().equals(field.getMappedByIdValue())) {
+                    pkCols =  ((ValueMappingImpl)fmds[i].getValue()).getColumns();
+                    break;
+                }
+            }
+        }
+        List cols = new ArrayList();
+        for (int i = 0; i < pkCols.length; i++) {
+            Column newCol = new Column();
+            newCol.setName(pkCols[i].getName());
+            cols.add(newCol);
+        }
+        return cols;
+    }
+
+    /**
      * Return whether our defining mapping is an unjoined subclass of
      * the type of the given value.
      */

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=741595&r1=741594&r2=741595&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 Fri
Feb  6 15:34:53 2009
@@ -346,6 +346,10 @@
             				(prev == null) ? null : prev.getClass(), prev,
             				(val == null) ? null : val.getClass(), val })).
             				setFatal(true);
+            	} else {
+            	    // since not allow to update and the new value is 0 or null,
+            	    // just return.
+            	    return;
             	}
             }
         }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java?rev=741595&r1=741594&r2=741595&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java
Fri Feb  6 15:34:53 2009
@@ -142,6 +142,7 @@
     private String _fullName = null;
     private String _embedFullName = null;
     private int _resMode = MODE_NONE;
+    private String _mappedByIdValue = null;
 
     // load/store info
     private String[] _comments = null;
@@ -2155,4 +2156,12 @@
     public void setElementCollection(boolean isElementCollection) {
         this._isElementCollection = isElementCollection;
     }
+
+    public String getMappedByIdValue() {
+        return _mappedByIdValue;
+    }
+
+    public void setMappedByIdValue(String mappedByIdValue) {
+        this._mappedByIdValue = mappedByIdValue;
+    }
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java?rev=741595&r1=741594&r2=741595&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
Fri Feb  6 15:34:53 2009
@@ -60,6 +60,7 @@
 import javax.persistence.ManyToOne;
 import javax.persistence.MapKey;
 import javax.persistence.MapKeyClass;
+import javax.persistence.MappedById;
 import javax.persistence.MappedSuperclass;
 import javax.persistence.NamedNativeQueries;
 import javax.persistence.NamedNativeQuery;
@@ -145,6 +146,7 @@
         _tags.put(IdClass.class, ID_CLASS);
         _tags.put(MapKey.class, MAP_KEY);
         _tags.put(MapKeyClass.class, MAP_KEY_CLASS);
+        _tags.put(MappedById.class, MAPPED_BY_ID);
         _tags.put(NamedNativeQueries.class, NATIVE_QUERIES);
         _tags.put(NamedNativeQuery.class, NATIVE_QUERY);
         _tags.put(NamedQueries.class, QUERIES);
@@ -1062,6 +1064,9 @@
                 case EMBEDDED_ID:
                     fmd.setPrimaryKey(true);
                     break;
+                case MAPPED_BY_ID:
+                    parseMappedById(fmd, (MappedById)anno);
+                    break;
                 case MAP_KEY:
                     if (isMappingOverrideMode())
                         parseMapKey(fmd, (MapKey) anno);
@@ -1419,6 +1424,17 @@
     }
 
     /**
+     * Parse @MappedById.
+     */
+    private void parseMappedById(FieldMetaData fmd, MappedById anno) {
+        String value = anno.value();
+        if (value != null)
+            fmd.setMappedByIdValue(value);
+        else
+            fmd.setMappedByIdValue("");
+    }
+    
+    /**
      * Setup the field as a LOB mapping.
      */
     protected void parseLobMapping(FieldMetaData fmd) {

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/MetaDataTag.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/MetaDataTag.java?rev=741595&r1=741594&r2=741595&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/MetaDataTag.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/MetaDataTag.java
Fri Feb  6 15:34:53 2009
@@ -45,6 +45,7 @@
     LOB,
     MAP_KEY,
     MAP_KEY_CLASS,
+    MAPPED_BY_ID,
     NATIVE_QUERIES,
     NATIVE_QUERY,
     QUERY_STRING,



Mime
View raw message