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 2008/11/13 19:33:07 UTC

svn commit: r713778 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/ openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persis...

Author: ppoddar
Date: Thu Nov 13 10:33:07 2008
New Revision: 713778

URL: http://svn.apache.org/viewvc?rev=713778&view=rev
Log:
OPENJPA-340: Let DBDictionary generate Unique Constraint names rather than Unique trying to auto-generate such names in lesser scope. Strengthen the tests to have exactly same named unique columns in two classes to verify that the unique constraint names are unique across tables.

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraintWithXMLDescriptor.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java?rev=713778&r1=713777&r2=713778&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java Thu Nov 13 10:33:07 2008
@@ -18,8 +18,6 @@
  */
 package org.apache.openjpa.jdbc.schema;
 
-import org.apache.commons.lang.StringUtils;
-
 /**
  * Represents a unique constraint. It can also represent a partial constraint.
  *
@@ -28,28 +26,19 @@
  */
 public class Unique
     extends LocalConstraint {
-    private boolean _autoNaming = false;
-    private static int _counter = 1;
+	
+	public Unique() {
+		super();
+	}
     
-	/**
-     * Default constructor without a name.
-     * Implies that this constraint will auto-generate its name from the names 
-     * of its columns, unless later the name is set explicitly.
-     */
-    public Unique() {
-    	_autoNaming = true;
-    }
-
     /**
      * Construct with given name.
-     * Implies that this constraint will not auto-generate its name.
      * 
      * @param name the name of the constraint, if any
      * @param table the table of the constraint
      */
     public Unique(String name, Table table) {
         super(name, table);
-    	_autoNaming = false;
     }
 
     public boolean isLogical() {
@@ -69,59 +58,17 @@
     
     /**
      * Set the name of the constraint. This method cannot be called if the
-     * constraint already belongs to a table. Calling this method also has the
-     * side-effect of implying that the instance will not auto-generate its
-     * name.
+     * constraint already belongs to a table. 
      */
     public void setName(String name) {
         super.setName(name);
-        _autoNaming = false;
     }
     
     /**
-     * Gets the name of the constraint. If no name has been set by the user
-     * then this method has the side-effect of auto-generating a name from
-     * the name of its columns.
-     */
-    public String getName() {
-    	if (getTable() == null && _autoNaming) {
-    		setName(createAutoName());
-    		_autoNaming = true;
-    	}
-    	return super.getName();
-    }
-
-    /**
      * Return true if the structure of this primary key matches that of
      * the given one (same table, same columns).
      */
     public boolean equalsUnique(Unique unq) {
         return equalsLocalConstraint(unq);
     }
-
-    /*
-     * Affirms if this instance is currently generating its own name. No 
-     * mutator because auto-naming is switched off as side-effect of user 
-     * calling setName() directly. 
-     */
-	public boolean isAutoNaming() {
-		return _autoNaming;
-	}
-	
-	private String createAutoName() {
-		Column[] columns = getColumns();
-		int l = 32/Math.max(columns.length,1);
-		StringBuffer autoName = new StringBuffer("UNQ_"); 
-		if (columns.length == 0) 
-			autoName.append(_counter++);
-		for (Column column : columns)
-			autoName.append(chop(column.getName(),l));
-		return autoName.toString();
-	}
-	
-    private String chop(String name, int head) {
-    	if (StringUtils.isEmpty(name))
-    		return name;
-    	return name.substring(0, Math.min(Math.max(1,head), name.length()));
-    }
 }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java?rev=713778&r1=713777&r2=713778&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java Thu Nov 13 10:33:07 2008
@@ -53,20 +53,20 @@
 		List<String> sqls = super.sql;
 		
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_A",
-				"UNIQUE .*\\(a1, a2\\)", 
-				"UNIQUE .*\\(a3, a4\\).*");
+				"UNIQUE .*\\(f1, f2\\)", 
+				"UNIQUE .*\\(f3, f4\\).*");
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_B",
-				"UNIQUE .*\\(b1, b2\\).*");
+				"UNIQUE .*\\(f1, f2\\).*");
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_SECONDARY",
-				"UNIQUE .*\\(sa1\\)");
+				"UNIQUE .*\\(sf1\\)");
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_GENERATOR",
 				"UNIQUE .*\\(GEN1, GEN2\\)");
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_JOINTABLE",
 				"UNIQUE .*\\(FK_A, FK_B\\)");
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_A",
-			    "UNIQUE .*\\(SAME\\)");
+			    "UNIQUE .*\\(f1\\)");
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_B",
-			    "UNIQUE .*\\(SAME\\)");
+			    "UNIQUE .*\\(f1\\)");
 	}
 		
 	void assertSQLFragnments(List<String> list, String... keys) {

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraintWithXMLDescriptor.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraintWithXMLDescriptor.java?rev=713778&r1=713777&r2=713778&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraintWithXMLDescriptor.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraintWithXMLDescriptor.java Thu Nov 13 10:33:07 2008
@@ -60,12 +60,12 @@
 		// Following verification techniques is fragile as databases DDL
 		// syntax vary greatly on UNIQUE CONSTRAINT
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_A_XML",
-				"UNIQUE .*\\(a1x, a2x\\)", 
-				"UNIQUE .*\\(a3x, a4x\\)");
+				"UNIQUE .*\\(f1x, f2x\\)", 
+				"UNIQUE .*\\(f3x, f4x\\)");
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_B_XML",
-				"UNIQUE .*\\(b1x, b2x\\)");
+				"UNIQUE .*\\(f1x, f2x\\)");
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_SECONDARY_XML",
-				"UNIQUE .*\\(sa1x\\)");
+				"UNIQUE .*\\(sf1x\\)");
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_GENERATOR_XML",
 				"UNIQUE .*\\(GEN1_XML, GEN2_XML\\)");
 		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_JOINTABLE_XML",

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java?rev=713778&r1=713777&r2=713778&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java Thu Nov 13 10:33:07 2008
@@ -31,52 +31,61 @@
 import javax.persistence.UniqueConstraint;
 
 /**
- * Data structures for testing unique constraint settings
- * on ORM Annotations.
- * @UniqueConstraint annotation is declared at class-level with @Table, 
- * @SecondaryTable annotations and at field-level with @JoinTable annotation.
+ * Data structures for testing unique constraint settings on ORM Annotations.
+ * @UniqueConstraint annotation is declared at class-level with 
+ *    @Table, 
+ *    @SecondaryTable annotations 
+ * and at field-level with 
+ *    @JoinTable annotation.
+ * also with
+ *    @Column(unique=true) on single column.
  * 
  * The columns included in unique constraint must be non-nullable. This is 
  * recommended that the non-nullability of the column is explictly set by the
  * user, though the implementation forces a column to non-nullable as a column
  * is included in a unique constraint.
  * 
+ * The name of the constraint is generated by the implementation as JPA ORM
+ * specification has not allowed to specify a name for the constraint via the
+ * annotation or XML descriptor. Some databases allow two constraints having the
+ * same name but applied to different tables, while some other databases do not.
+ *   
+ * 
  * @author Pinaki Poddar
  *
  */
 @Entity
 @Table(name="UNIQUE_A",
-	   uniqueConstraints={@UniqueConstraint(columnNames={"a1","a2"}),
-		                  @UniqueConstraint(columnNames={"a3","a4"})})
+	   uniqueConstraints={@UniqueConstraint(columnNames={"f1","f2"}),
+		                  @UniqueConstraint(columnNames={"f3","f4"})})
 @SecondaryTable(name="UNIQUE_SECONDARY",
-		uniqueConstraints=@UniqueConstraint(columnNames={"sa1"}))
+		uniqueConstraints=@UniqueConstraint(columnNames={"sf1"}))
 
 public class UniqueA {
 	@Id
 	private int aid;
 
+	// Same named field in UniqueB also is defined as unique
 	@Column(unique=true, nullable=false)
-	private int a1;
+	private int f1;
 	
 	@Column(nullable=false)
-	private int a2;
+	private int f2;
 	
 	@Column(nullable=false)
-	private int a3;
+	private int f3;
 	
 	@Column(nullable=false)
-	private int a4;
-	
-	@Column(name="SAME", unique=true)
-	private int same;
+	private int f4;
 	
-	private int a5;
-	private int a6;
+	private int f5;
+	private int f6;
 	
 	@Column(table="UNIQUE_SECONDARY", nullable=false)
-	private short sa1;
+	private short sf1;
+	
 	@Column(table="UNIQUE_SECONDARY")
-	private short sa2;
+	private short sf2;
 	
 	@ManyToMany
 	@JoinTable(name="UNIQUE_JOINTABLE",

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java?rev=713778&r1=713777&r2=713778&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java Thu Nov 13 10:33:07 2008
@@ -22,7 +22,7 @@
 
 @Entity
 @Table(name="UNIQUE_B",
-	   uniqueConstraints={@UniqueConstraint(columnNames={"b1","b2"})})
+	   uniqueConstraints={@UniqueConstraint(columnNames={"f1","f2"})})
 public class UniqueB {
 	@Id
 	@GeneratedValue(strategy=GenerationType.TABLE, generator="testGenerator")
@@ -31,10 +31,10 @@
 			uniqueConstraints={@UniqueConstraint(columnNames={"GEN1","GEN2"})})
 	private int bid;
 	
+	// Same named field in UniqueA also is defined as unique
+	@Column(unique=true, nullable=false)
+	private int f1;
+	
 	@Column(nullable=false)
-	private int b1;
-	@Column(nullable=false)
-	private int b2;
-	@Column(name="SAME", unique=true)
-	private int same;
+	private int f2;
 }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml?rev=713778&r1=713777&r2=713778&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml Thu Nov 13 10:33:07 2008
@@ -19,7 +19,7 @@
 -->
 <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
+	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence orm_1_0.xsd"
 	version="1.0">
 	
 	<persistence-unit-metadata>
@@ -31,48 +31,45 @@
 	<entity name="UniqueA" class="UniqueA">
 		<table name="UNIQUE_A_XML">
 			<unique-constraint>
-				<column-name>a1x</column-name>
-				<column-name>a2x</column-name>
+				<column-name>f1x</column-name>
+				<column-name>f2x</column-name>
 			</unique-constraint>
 			<unique-constraint>
-				<column-name>a3x</column-name>
-				<column-name>a4x</column-name>
+				<column-name>f3x</column-name>
+				<column-name>f4x</column-name>
 			</unique-constraint>
 		</table>
 		<secondary-table name="UNIQUE_SECONDARY_XML">
 			<unique-constraint>
-				<column-name>sa1x</column-name>
+				<column-name>sf1x</column-name>
 			</unique-constraint>
 		</secondary-table>
 		<attributes>
 			<id name="aid">
 			</id>
-			<basic name="a1">
-				<column name="a1x"/>
+			<basic name="f1">
+				<column name="f1x" unique="true"/>
 			</basic>
-			<basic name="a2">
-				<column name="a2x"/>
+			<basic name="f2">
+				<column name="f2x"/>
 			</basic>
-			<basic name="a3">
-				<column name="a3x"/>
+			<basic name="f3">
+				<column name="f3x"/>
 			</basic>
-			<basic name="a4">
-				<column name="a4x"/>
+			<basic name="f4">
+				<column name="f4x"/>
 			</basic>
-			<basic name="a5">
-				<column name="a5x"/>
+			<basic name="f5">
+				<column name="f5x"/>
 			</basic>
-			<basic name="a6">
-				<column name="a6x"/>
+			<basic name="f6">
+				<column name="f6x"/>
 			</basic>
-			<basic name="same">
-				<column name="SAME" unique="true"/>
+			<basic name="sf1">
+				<column name="sf1x" table="UNIQUE_SECONDARY_XML" />
 			</basic>
-			<basic name="sa1">
-				<column name="sa1x" table="UNIQUE_SECONDARY_XML" />
-			</basic>
-			<basic name="sa2">
-				<column name="sa2x" table="UNIQUE_SECONDARY_XML" />
+			<basic name="sf2">
+				<column name="sf2x" table="UNIQUE_SECONDARY_XML" />
 			</basic>
 
 			<many-to-many name="bs">
@@ -91,8 +88,8 @@
 	<entity name="UniqueB" class="UniqueB">
 		<table name="UNIQUE_B_XML">
 			<unique-constraint>
-				<column-name>b1x</column-name>
-				<column-name>b2x</column-name>
+				<column-name>f1x</column-name>
+				<column-name>f2x</column-name>
 			</unique-constraint>
 		</table>
 		<attributes>
@@ -108,15 +105,12 @@
 					</unique-constraint>
 				</table-generator>
 			</id>
-			<basic name="b1">
-				<column name="b1x"/>
+			<basic name="f1">
+				<column name="f1x" unique="true"/>
 			</basic>
-			<basic name="b2">
-				<column name="b2x"/>
+			<basic name="f2">
+				<column name="f2x"/>
 			</basic>
-            <basic name="same">
-                <column name="SAME" unique="true"/>
-            </basic>
 		</attributes>
 	</entity>
 </entity-mappings>