You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mt...@apache.org on 2009/10/21 21:35:51 UTC

svn commit: r828158 - in /openjpa/branches/1.3.x: 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/openj...

Author: mtylenda
Date: Wed Oct 21 19:35:50 2009
New Revision: 828158

URL: http://svn.apache.org/viewvc?rev=828158&view=rev
Log:
OPENJPA-1165: Avoid duplicate unique constraint names - fix ported from trunk

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

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java?rev=828158&r1=828157&r2=828158&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java (original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java Wed Oct 21 19:35:50 2009
@@ -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,27 +26,19 @@
  */
 public class Unique
     extends LocalConstraint {
-	private boolean _autoNaming = false;
+	
+	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() {
@@ -68,57 +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
+     * Return true if the structure of this unique constraint 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_");
-		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/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java?rev=828158&r1=828157&r2=828158&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java (original)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java Wed Oct 21 19:35:50 2009
@@ -53,18 +53,23 @@
 		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 .*\\(f1\\)",
+                "UNIQUE .*\\(f5\\)");
+		assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_B",
+			    "UNIQUE .*\\(f1\\)");
 	}
-	
+		
 	void assertSQLFragnments(List<String> list, String... keys) {
 		if (SQLSniffer.matches(list, keys))
 			return;

Modified: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraintWithXMLDescriptor.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraintWithXMLDescriptor.java?rev=828158&r1=828157&r2=828158&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraintWithXMLDescriptor.java (original)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraintWithXMLDescriptor.java Wed Oct 21 19:35:50 2009
@@ -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/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java?rev=828158&r1=828157&r2=828158&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java (original)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java Wed Oct 21 19:35:50 2009
@@ -31,50 +31,62 @@
 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;
-	
+	private int f4;
 	
-	private int a5;
-	private int a6;
+    @Column(unique=true, nullable=false)
+	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/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java?rev=828158&r1=828157&r2=828158&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java (original)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java Wed Oct 21 19:35:50 2009
@@ -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,8 +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;
+	private int f2;
 }

Modified: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml?rev=828158&r1=828157&r2=828158&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml (original)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml Wed Oct 21 19:35:50 2009
@@ -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,46 +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="sa1">
-				<column name="sa1x" table="UNIQUE_SECONDARY_XML" />
+			<basic name="sf1">
+				<column name="sf1x" 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">
@@ -89,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>
@@ -106,11 +105,11 @@
 					</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>
 		</attributes>
 	</entity>