openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r725770 - in /openjpa/trunk: openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/
Date Thu, 11 Dec 2008 18:42:10 GMT
Author: faywang
Date: Thu Dec 11 10:42:10 2008
New Revision: 725770

URL: http://svn.apache.org/viewvc?rev=725770&view=rev
Log:
OPENJPA-782: add orm support for CollectionTable and
ElementCollection 

Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/MappingTag.java
    openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceStrategy.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/MappingTag.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/MappingTag.java?rev=725770&r1=725769&r2=725770&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/MappingTag.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/MappingTag.java
Thu Dec 11 10:42:10 2008
@@ -36,6 +36,7 @@
     ATTR_OVERRIDE,
     ATTR_OVERRIDES,
     COL,
+    COLLECTION_TABLE,
     COLUMN_NAME,
     COLUMN_RESULT,
     DISCRIM_COL,
@@ -61,7 +62,6 @@
     CLASS_CRIT,
     COLS,
     CONTAINER_TABLE,
-    COLLECTION_TABLE,
     DATASTORE_ID_COL,
     DISCRIM_STRAT,
     EAGER_FETCH_MODE,

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java?rev=725770&r1=725769&r2=725770&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java
Thu Dec 11 10:42:10 2008
@@ -32,16 +32,12 @@
 import javax.persistence.TemporalType;
 
 import org.apache.commons.lang.StringUtils;
-import org.xml.sax.Attributes;
-import org.xml.sax.Locator;
-import org.xml.sax.SAXException;
 import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
 import org.apache.openjpa.jdbc.meta.ClassMapping;
 import org.apache.openjpa.jdbc.meta.ClassMappingInfo;
 import org.apache.openjpa.jdbc.meta.DiscriminatorMappingInfo;
 import org.apache.openjpa.jdbc.meta.FieldMapping;
 import org.apache.openjpa.jdbc.meta.FieldMappingInfo;
-import org.apache.openjpa.jdbc.meta.MappingInfo;
 import org.apache.openjpa.jdbc.meta.MappingRepository;
 import org.apache.openjpa.jdbc.meta.QueryResultMapping;
 import org.apache.openjpa.jdbc.meta.SequenceMapping;
@@ -51,15 +47,18 @@
 import org.apache.openjpa.jdbc.meta.strats.NoneClassStrategy;
 import org.apache.openjpa.jdbc.meta.strats.VerticalClassStrategy;
 import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.schema.Table;
 import org.apache.openjpa.jdbc.schema.Unique;
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.ClassMetaData;
 import org.apache.openjpa.meta.FieldMetaData;
 import org.apache.openjpa.meta.JavaTypes;
-import org.apache.openjpa.meta.MetaDataRepository;
 import org.apache.openjpa.persistence.XMLPersistenceMetaDataParser;
 import org.apache.openjpa.util.InternalException;
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
 
 import static org.apache.openjpa.persistence.jdbc.MappingTag.*;
 import serp.util.Numbers;
@@ -78,6 +77,7 @@
     static {
         _elems.put("association-override", ASSOC_OVERRIDE);
         _elems.put("attribute-override", ATTR_OVERRIDE);
+        _elems.put("collection-table", COLLECTION_TABLE);
         _elems.put("column", COL);
         _elems.put("column-name", COLUMN_NAME);
         _elems.put("column-result", COLUMN_RESULT);
@@ -254,6 +254,9 @@
             case COLUMN_NAME:
                 ret = true;
                 break;
+            case COLLECTION_TABLE:
+                ret = startCollectionTable(attrs);
+                break;
             default:
                 ret = false;
         }
@@ -651,9 +654,11 @@
     private FieldMapping getAttributeOverride(FieldMapping fm)
         throws SAXException {
         ClassMapping embed = fm.getEmbeddedMapping();
-        if (embed == null)
-            throw getException(_loc.get("not-embedded", fm));
-
+        if (embed == null) {
+            embed = fm.getElementMapping().getEmbeddedMapping();
+            if (embed == null)
+                throw getException(_loc.get("not-embedded", fm));
+        }
         FieldMapping efm = embed.getFieldMapping(_override);
         if (efm == null)
             throw getException(_loc.get("embed-override-name",
@@ -732,7 +737,17 @@
         throws SAXException {
         // only join cols in a join table join field table to class table;
         // others act as data fk cols
-        if (currentParent() != JOIN_TABLE)
+        Object currentParent = currentParent();
+        if (currentParent == COLLECTION_TABLE) {
+            FieldMapping fm = (FieldMapping) peekElement();
+            Column col = parseColumn(attrs);
+            List colList = new ArrayList();
+            colList.add(col);
+            fm.getMappingInfo().setColumns(colList);
+            return true;
+        }
+        
+        if (currentParent != JOIN_TABLE)
             return startColumn(attrs);
 
         if (_joinCols == null)
@@ -746,9 +761,26 @@
      */
     private boolean startColumn(Attributes attrs)
         throws SAXException {
+        Column col = parseColumn(attrs);
+        Object obj = peekElement();
+        if (obj instanceof FieldMapping) {
+            FieldMapping fm = (FieldMapping)obj;
+            // a collection of basic types
+            // the column is in a separate table
+            if (fm.isElementCollection() &&
+                fm.getElementMapping().getEmbeddedMapping() == null) {
+                List list = fm.getElementMapping().getValueInfo().getColumns();
+                if (list.size() == 0) {
+                    list = new ArrayList();
+                    fm.getElementMapping().getValueInfo().setColumns(list);
+                }
+                list.add(col);
+                return true;
+            }
+        }
         if (_cols == null)
             _cols = new ArrayList<Column>(3);
-        _cols.add(parseColumn(attrs));
+        _cols.add(col);
         return true;
     }
 
@@ -800,6 +832,35 @@
     }
 
     /**
+     * Parse collectionTable.
+     */
+    private boolean startCollectionTable(Attributes attrs)
+        throws SAXException {
+        FieldMapping fm = (FieldMapping) peekElement();
+
+        FieldMappingInfo info = fm.getMappingInfo();
+        Table ctbl = parseCollectionTable(attrs);
+        info.setTableName(toTableName(ctbl.getSchemaName(),
+            ctbl.getName()));
+        return true;
+    }
+
+    private Table parseCollectionTable(Attributes attrs) {
+        Table table = new Table();
+        String val = attrs.getValue("name");
+        if (val != null)
+            table.setName(val);
+        val = attrs.getValue("schema");
+        if (val != null)
+            table.setSchemaName(val);
+        //val = attrs.getValue("catalog");
+        //if (val != null)
+        //    table.setCatalog(val);
+                
+        return table; 
+    }
+  
+    /**
      * Return a table name for the given attributes.
      */
     private String toTableName(String schema, String table) {

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceStrategy.java?rev=725770&r1=725769&r2=725770&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceStrategy.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceStrategy.java
Thu Dec 11 10:42:10 2008
@@ -32,6 +32,7 @@
 public enum PersistenceStrategy {
     // order matters to match XML spec
     BASIC,
+    ELEM_COLL,
     MANY_ONE,
     ONE_MANY,
     ONE_ONE,
@@ -41,6 +42,5 @@
     // openjpa extensions
     PERS,
     PERS_COLL,
-    ELEM_COLL,
     PERS_MAP,
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java?rev=725770&r1=725769&r2=725770&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java
Thu Dec 11 10:42:10 2008
@@ -31,6 +31,7 @@
 import java.util.Set;
 import java.util.Stack;
 import javax.persistence.CascadeType;
+import javax.persistence.FetchType;
 import javax.persistence.GenerationType;
 import static javax.persistence.CascadeType.*;
 
@@ -67,6 +68,7 @@
 import static org.apache.openjpa.persistence.MetaDataTag.*;
 import static org.apache.openjpa.persistence.PersistenceStrategy.*;
 import org.apache.openjpa.util.ImplHelper;
+import org.apache.openjpa.util.MetaDataException;
 
 import serp.util.Numbers;
 
@@ -151,6 +153,7 @@
         _elems.put("one-to-many", ONE_MANY);
         _elems.put("many-to-many", MANY_MANY);
         _elems.put("transient", TRANSIENT);
+        _elems.put("element-collection", ELEM_COLL);
     }
 
     private static final Localizer _loc = Localizer.forPackage
@@ -356,6 +359,13 @@
     }
 
     /**
+     * Peek a parse element from the stack.
+     */
+    protected Object peekElement() {
+        return _elements.peek();
+    }
+    
+    /**
      * Return the current element being parsed. May be a class metadata,
      * field metadata, query metadata, etc.
      */
@@ -1276,6 +1286,8 @@
             case TRANSIENT:
                 fmd.setManagement(FieldMetaData.MANAGE_NONE);
                 break;
+            case ELEM_COLL:
+                parseElementCollection(fmd, attrs);
         }
     }
 
@@ -1378,6 +1390,27 @@
         fmd.setSerialized(false); // override any Lob annotation
     }
 
+    protected void parseElementCollection(FieldMetaData fmd, Attributes attrs)
+        throws SAXException {
+        String val = attrs.getValue("target-entity");
+        if (val != null)
+            fmd.getElement().setDeclaredType(classForName(val));
+
+        if (fmd.getDeclaredTypeCode() != JavaTypes.COLLECTION &&
+            fmd.getDeclaredTypeCode() != JavaTypes.MAP)
+            throw getException(_loc.get("bad-meta-anno", fmd, "ElementCollection"));
+
+        val = attrs.getValue("fetch");
+        if (val != null)
+            fmd.setInDefaultFetchGroup("EAGER".equals(val));
+        fmd.setElementCollection(true);
+        if (JavaTypes.maybePC(fmd.getElement())) {
+            fmd.getElement().setEmbedded(true);
+            if (fmd.getElement().getEmbeddedMetaData() == null)
+                fmd.getElement().addEmbeddedMetaData();
+        }
+    }
+    
     /**
      * Parse map-key.
      */



Mime
View raw message