You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pp...@apache.org on 2007/01/03 18:17:39 UTC

svn commit: r492225 - in /incubator/openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/ openjpa-persistence-jdb...

Author: ppoddar
Date: Wed Jan  3 09:17:38 2007
New Revision: 492225

URL: http://svn.apache.org/viewvc?view=rev&rev=492225
Log:
Adding @UniqueConstraint annotation.


Modified:
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMappingInfo.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/XMLSchemaParser.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java?view=diff&rev=492225&r1=492224&r2=492225
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java (original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java Wed Jan  3 09:17:38 2007
@@ -36,6 +36,8 @@
 import org.apache.openjpa.jdbc.schema.ForeignKey;
 import org.apache.openjpa.jdbc.schema.Schemas;
 import org.apache.openjpa.jdbc.schema.Table;
+import org.apache.openjpa.jdbc.schema.Unique;
+import org.apache.openjpa.jdbc.schema.XMLSchemaParser;
 import org.apache.openjpa.jdbc.sql.Joins;
 import org.apache.openjpa.jdbc.sql.Result;
 import org.apache.openjpa.jdbc.sql.RowManager;
@@ -791,6 +793,44 @@
                     _cols[i].setFlag(Column.FLAG_DIRECT_INSERT, true);
                 if (io.isUpdatable(i, false))
                     _cols[i].setFlag(Column.FLAG_DIRECT_UPDATE, true);
+            }
+        }
+        mapUniqueConstraints();
+    }
+    
+    /**
+     * Adds unique constraints to the mapped table.
+     *
+     */
+    void mapUniqueConstraints() {
+        Log log = getRepository().getLog();
+        Collection uniqueInfos = _info.getUniqueConstraints();
+        if (uniqueInfos == null || uniqueInfos.isEmpty())
+            return;
+        Iterator iter = uniqueInfos.iterator();
+        Table table = getTable();
+        int i = 1;
+        while (iter.hasNext()) {
+            XMLSchemaParser.UniqueInfo uniqueInfo = 
+                (XMLSchemaParser.UniqueInfo)iter.next();
+            if (uniqueInfo.cols == null || uniqueInfo.cols.isEmpty())
+                continue;
+            String constraintName = table.getName() + "_UNIQUE_" + i;
+            i++;
+            Unique uniqueConstraint = table.addUnique(constraintName);
+            Iterator uniqueColumnNames = uniqueInfo.cols.iterator();
+            while (uniqueColumnNames.hasNext()) {
+                String uniqueColumnName = (String)uniqueColumnNames.next();
+                Column uniqueColumn = table.getColumn(uniqueColumnName);
+                if (uniqueColumn != null) {
+                    uniqueConstraint.addColumn(uniqueColumn);
+                } else {
+                    table.removeUnique(uniqueConstraint);
+                    if (log.isWarnEnabled())
+                        log.warn(_loc.get("missing-unique-column", this, 
+                            table.getName(), uniqueColumnName));
+                    break;
+                }
             }
         }
     }

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMappingInfo.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMappingInfo.java?view=diff&rev=492225&r1=492224&r2=492225
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMappingInfo.java (original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMappingInfo.java Wed Jan  3 09:17:38 2007
@@ -21,6 +21,9 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Collection;
+import java.util.ArrayList;
+import java.util.Arrays;
 
 import org.apache.openjpa.jdbc.meta.strats.FullClassStrategy;
 import org.apache.openjpa.jdbc.schema.Column;
@@ -28,6 +31,7 @@
 import org.apache.openjpa.jdbc.schema.Schema;
 import org.apache.openjpa.jdbc.schema.SchemaGroup;
 import org.apache.openjpa.jdbc.schema.Table;
+import org.apache.openjpa.jdbc.schema.XMLSchemaParser;
 import org.apache.openjpa.lib.meta.SourceTracker;
 import org.apache.openjpa.lib.xml.Commentable;
 
@@ -51,6 +55,7 @@
     private File _file = null;
     private int _srcType = SRC_OTHER;
     private String[] _comments = null;
+    private Collection _uniqueConstraints = null;//XMLSchemaParser.UniqueInfo
 
     /**
      * The described class name.
@@ -275,7 +280,7 @@
             : cls.getStrategy().getAlias();
         if (strat != null && (cls.getPCSuperclass() != null
             || !FullClassStrategy.ALIAS.equals(strat)))
-            setStrategy(strat);
+            setStrategy(strat);        
     }
 
     public boolean hasSchemaComponents() {
@@ -308,8 +313,22 @@
                     _seconds.put(key, cinfo._seconds.get(key));
             }
         }
+        if (cinfo._uniqueConstraints != null)
+           _uniqueConstraints = new ArrayList(cinfo._uniqueConstraints);
     }
 
+    public void addUniqueConstaint(String[] columnNames) {
+        if (_uniqueConstraints == null)
+            _uniqueConstraints = new ArrayList();
+        XMLSchemaParser.UniqueInfo uniqueInfo = new XMLSchemaParser.UniqueInfo();
+        uniqueInfo.cols = Arrays.asList(columnNames);
+        _uniqueConstraints.add(uniqueInfo);
+    }
+    
+    public Collection getUniqueConstraints() {
+        return _uniqueConstraints;
+    }
+    
     public File getSourceFile() {
         return _file;
     }

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java?view=diff&rev=492225&r1=492224&r2=492225
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java (original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java Wed Jan  3 09:17:38 2007
@@ -560,6 +560,27 @@
             }
         }
 
+        // Unique Constraints on group of columns
+        Unique[] uniques;
+        for (int i = 0; i < schemas.length; i++) {
+            tabs = schemas[i].getTables();
+            for (int j = 0; j < tabs.length; j++) {
+                // create unique constraints only on new tables 
+                if (!newTables.contains(tabs[j]))
+                    continue;
+
+                uniques = tabs[j].getUniques();
+                if (uniques == null || uniques.length == 0)
+                    continue;
+                dbTable = db.findTable(tabs[j]);
+                if (dbTable == null)
+                    continue;
+                for (int k = 0; k < uniques.length; k++) {
+                    dbTable.importUnique(uniques[k]);
+                }
+            }
+        }
+        
         // foreign keys
         ForeignKey[] fks;
         ForeignKey fk;

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/XMLSchemaParser.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/XMLSchemaParser.java?view=diff&rev=492225&r1=492224&r2=492225
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/XMLSchemaParser.java (original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/XMLSchemaParser.java Wed Jan  3 09:17:38 2007
@@ -579,7 +579,7 @@
     /**
      * Used to hold unique constraint info before it is resolved.
      */
-    private static class UniqueInfo {
+    public static class UniqueInfo {
 
         public Unique unq = null;
         public Collection cols = new LinkedList();

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties?view=diff&rev=492225&r1=492224&r2=492225
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties (original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties Wed Jan  3 09:17:38 2007
@@ -401,3 +401,6 @@
 	attempts to traverse through a non-relation field.
 num-cols-path: Result path "{2}" in result type "{1}" of mapping "{0}" \
 	attempts to map a field that does not have exactly 1 column.
+missing-unique-column: A unique constraint includes a column "{2}" specified \
+	in mapping of class "{0}" to table "{1}". However, the column does not \
+	exist in "{1}" table. This constraint will not be defined in the schema.

Modified: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java?view=diff&rev=492225&r1=492224&r2=492225
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java (original)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java Wed Jan  3 09:17:38 2007
@@ -47,6 +47,7 @@
 import javax.persistence.Table;
 import javax.persistence.TableGenerator;
 import javax.persistence.Temporal;
+import javax.persistence.UniqueConstraint;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
@@ -459,10 +460,9 @@
         if (tableName != null)
             cm.getMappingInfo().setTableName(tableName);
 
-        //### EJB3
-        Log log = getLog();
-        if (table.uniqueConstraints().length > 0 && log.isWarnEnabled())
-            log.warn(_loc.get("unique-constraints", cm));
+        for (UniqueConstraint unique:table.uniqueConstraints()) {
+            ((ClassMappingInfo)cm.getMappingInfo()).addUniqueConstaint(unique.columnNames());
+        }
     }
 
     /**



Re: svn commit: r492225 - in /incubator/openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/ openjpa-persistence-jdb...

Posted by Abe White <aw...@bea.com>.
I don't agree with this implementation.  It doesn't leave any room  
for customization through MappingDefaults, it ties the ClassMapping  
to the XMLSchemaParser (?!), and it's totally different than our  
mapping of indexes, foreign keys, and primary keys, the other  
supported constraint types.

> +        mapUniqueConstraints();
> +    }
> +
> +    /**
> +     * Adds unique constraints to the mapped table.
> +     *
> +     */
> +    void mapUniqueConstraints() {
> +        Log log = getRepository().getLog();
> +        Collection uniqueInfos = _info.getUniqueConstraints();
> +        if (uniqueInfos == null || uniqueInfos.isEmpty())
> +            return;
> +        Iterator iter = uniqueInfos.iterator();
> +        Table table = getTable();
> +        int i = 1;
> +        while (iter.hasNext()) {
> +            XMLSchemaParser.UniqueInfo uniqueInfo =
> +                (XMLSchemaParser.UniqueInfo)iter.next();
> +            if (uniqueInfo.cols == null || uniqueInfo.cols.isEmpty())
> +                continue;
> +            String constraintName = table.getName() + "_UNIQUE_" + i;
> +            i++;
> +            Unique uniqueConstraint = table.addUnique 
> (constraintName);
> +            Iterator uniqueColumnNames = uniqueInfo.cols.iterator();
> +            while (uniqueColumnNames.hasNext()) {
> +                String uniqueColumnName = (String) 
> uniqueColumnNames.next();
> +                Column uniqueColumn = table.getColumn 
> (uniqueColumnName);
> +                if (uniqueColumn != null) {
> +                    uniqueConstraint.addColumn(uniqueColumn);
> +                } else {
> +                    table.removeUnique(uniqueConstraint);
> +                    if (log.isWarnEnabled())
> +                        log.warn(_loc.get("missing-unique-column",  
> this,
> +                            table.getName(), uniqueColumnName));
> +                    break;
> +                }
>              }
>          }
>      }
_______________________________________________________________________
Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it.