diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java index 3c8719143..a155c838a 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Unique.java @@ -28,7 +28,8 @@ import org.apache.commons.lang.StringUtils; */ public class Unique extends LocalConstraint { - private boolean _autoNaming = false; + private boolean _autoNaming = false; + private static int _counter = 1; /** * Default constructor without a name. @@ -110,7 +111,9 @@ public class Unique private String createAutoName() { Column[] columns = getColumns(); int l = 32/Math.max(columns.length,1); - StringBuffer autoName = new StringBuffer("UNQ_"); + 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(); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java index 9c004940e..ca6a51cfa 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestUniqueConstraint.java @@ -63,8 +63,12 @@ public class TestUniqueConstraint extends SQLListenerTestCase { "UNIQUE .*\\(GEN1, GEN2\\)"); assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_JOINTABLE", "UNIQUE .*\\(FK_A, FK_B\\)"); + assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_A", + "UNIQUE .*\\(SAME\\)"); + assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_B", + "UNIQUE .*\\(SAME\\)"); } - + void assertSQLFragnments(List list, String... keys) { if (SQLSniffer.matches(list, keys)) return; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java index d6a558245..c94b43760 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueA.java @@ -67,6 +67,8 @@ public class UniqueA { @Column(nullable=false) private int a4; + @Column(name="SAME", unique=true) + private int same; private int a5; private int a6; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java index db39129af..bf6e184dc 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/UniqueB.java @@ -35,4 +35,6 @@ public class UniqueB { private int b1; @Column(nullable=false) private int b2; + @Column(name="SAME", unique=true) + private int same; } diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml index 1093cc07e..4a07e506d 100644 --- a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/jdbc/unique/orm.xml @@ -65,7 +65,9 @@ - + + + @@ -112,6 +114,9 @@ + + +