openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r684442 - in /openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc: kernel/JDBCStoreQuery.java meta/FieldMapping.java meta/strats/RelationCollectionTableFieldStrategy.java
Date Sun, 10 Aug 2008 10:11:51 GMT
Author: ppoddar
Date: Sun Aug 10 03:11:50 2008
New Revision: 684442

URL: http://svn.apache.org/viewvc?rev=684442&view=rev
Log:
OPENJPA-692: Bi-directional mapping with JoinTable

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationCollectionTableFieldStrategy.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java?rev=684442&r1=684441&r2=684442&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java
Sun Aug 10 03:11:50 2008
@@ -45,6 +45,7 @@
 import org.apache.openjpa.jdbc.meta.FieldMapping;
 import org.apache.openjpa.jdbc.meta.strats.VerticalClassStrategy;
 import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.schema.ForeignKey;
 import org.apache.openjpa.jdbc.schema.Table;
 import org.apache.openjpa.jdbc.sql.DBDictionary;
 import org.apache.openjpa.jdbc.sql.SQLBuffer;
@@ -590,6 +591,8 @@
             else if (table != columns[i].getTable())
                 return INVALID;
         }
+        if (fm.isBidirectionalJoinTableMappingOwner())
+        	return INVALID;
         return table;
     }
 

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java?rev=684442&r1=684441&r2=684442&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java
Sun Aug 10 03:11:50 2008
@@ -44,6 +44,7 @@
 import org.apache.openjpa.meta.JavaTypes;
 import org.apache.openjpa.util.InternalException;
 import org.apache.openjpa.util.MetaDataException;
+import org.apache.tools.ant.taskdefs.condition.IsReference;
 
 /**
  * Specialization of metadata for relational databases.
@@ -71,6 +72,7 @@
     private boolean _outer = false;
     private int _fetchMode = Integer.MAX_VALUE;
     private Unique[] _joinTableUniques; // Unique constraints on JoinTable
+    private Boolean _bidirectionalJoinTableOwner = null;
     
     /**
      * Constructor.
@@ -1047,4 +1049,46 @@
     public void copyMappingInfo(ValueMapping vm) {
         _val.copyMappingInfo(vm);
     }
+    
+    /**
+     * Affirms if this field is the owning side of a bidirectional relation
+     * with a join table. Evaluated only once and the result cached for 
+     * subsequent call. Hence must be called after resolution.
+     */
+    public boolean isBidirectionalJoinTableMappingOwner() {
+    	if (_bidirectionalJoinTableOwner != null)
+    		return _bidirectionalJoinTableOwner.booleanValue();
+    	
+    	_bidirectionalJoinTableOwner = false;
+        ForeignKey fk = getForeignKey();
+        if (fk != null) 
+        	return false;
+        ForeignKey jfk = getJoinForeignKey();
+        if (jfk == null) 
+        	return false;
+        FieldMapping mappedBy = getValueMappedByMapping();
+        if (mappedBy != null) 
+        	return false;
+        ValueMapping elem = getElementMapping();
+        if (elem == null) 
+        	return false;
+        ClassMapping relType = elem.getDeclaredTypeMapping();
+        if (relType == null) 
+        	return false;
+        FieldMetaData[] relFmds = relType.getFields();
+        for (int i=0; i<relFmds.length;i++) {
+            if (relFmds[i].getDeclaredTypeMetaData() == getDeclaringMapping()) {
+                FieldMapping rfm = (FieldMapping)relFmds[i];
+        		ForeignKey rjfk = rfm.getJoinForeignKey();
+        		if (rjfk == null) 
+        		    continue;
+        		if (rjfk.getTable() == jfk.getTable()
+        		 && jfk.getTable().getColumns().length 
+        		 == jfk.getColumns().length + rjfk.getColumns().length) {
+        			_bidirectionalJoinTableOwner = true;
+        		}
+        	}
+        }
+        return _bidirectionalJoinTableOwner.booleanValue();
+    }
 }

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationCollectionTableFieldStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationCollectionTableFieldStrategy.java?rev=684442&r1=684441&r2=684442&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationCollectionTableFieldStrategy.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationCollectionTableFieldStrategy.java
Sun Aug 10 03:11:50 2008
@@ -29,6 +29,7 @@
 import org.apache.openjpa.jdbc.schema.ForeignKey;
 import org.apache.openjpa.jdbc.sql.Joins;
 import org.apache.openjpa.jdbc.sql.Result;
+import org.apache.openjpa.jdbc.sql.RowManager;
 import org.apache.openjpa.jdbc.sql.Select;
 import org.apache.openjpa.kernel.OpenJPAStateManager;
 import org.apache.openjpa.lib.util.Localizer;
@@ -93,4 +94,22 @@
             throw new MetaDataException(_loc.get("not-coll", field));
         super.map(adapt);
     }
+    
+    public void insert(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
+        throws SQLException {
+    	if (!field.isBidirectionalJoinTableMappingOwner())
+    		super.insert(sm, store, rm);
+    }
+    
+    public void update(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
+        throws SQLException {
+    	if (!field.isBidirectionalJoinTableMappingOwner())
+    		super.update(sm, store, rm);
+    }
+    
+    public void delete(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
+        throws SQLException {
+    	if (!field.isBidirectionalJoinTableMappingOwner())
+    		super.delete(sm, store, rm);
+    }
 }



Mime
View raw message