Generate unique name for unique constraints created by @Column(unique=true)

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@712300 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2008-11-07 22:49:13 +00:00
parent ea9378fc96
commit e11d0f7e12
5 changed files with 20 additions and 4 deletions

View File

@ -28,7 +28,8 @@ import org.apache.commons.lang.StringUtils;
*/ */
public class Unique public class Unique
extends LocalConstraint { extends LocalConstraint {
private boolean _autoNaming = false; private boolean _autoNaming = false;
private static int _counter = 1;
/** /**
* Default constructor without a name. * Default constructor without a name.
@ -110,7 +111,9 @@ public class Unique
private String createAutoName() { private String createAutoName() {
Column[] columns = getColumns(); Column[] columns = getColumns();
int l = 32/Math.max(columns.length,1); 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) for (Column column : columns)
autoName.append(chop(column.getName(),l)); autoName.append(chop(column.getName(),l));
return autoName.toString(); return autoName.toString();

View File

@ -63,8 +63,12 @@ public class TestUniqueConstraint extends SQLListenerTestCase {
"UNIQUE .*\\(GEN1, GEN2\\)"); "UNIQUE .*\\(GEN1, GEN2\\)");
assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_JOINTABLE", assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_JOINTABLE",
"UNIQUE .*\\(FK_A, FK_B\\)"); "UNIQUE .*\\(FK_A, FK_B\\)");
assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_A",
"UNIQUE .*\\(SAME\\)");
assertSQLFragnments(sqls, "CREATE TABLE UNIQUE_B",
"UNIQUE .*\\(SAME\\)");
} }
void assertSQLFragnments(List<String> list, String... keys) { void assertSQLFragnments(List<String> list, String... keys) {
if (SQLSniffer.matches(list, keys)) if (SQLSniffer.matches(list, keys))
return; return;

View File

@ -67,6 +67,8 @@ public class UniqueA {
@Column(nullable=false) @Column(nullable=false)
private int a4; private int a4;
@Column(name="SAME", unique=true)
private int same;
private int a5; private int a5;
private int a6; private int a6;

View File

@ -35,4 +35,6 @@ public class UniqueB {
private int b1; private int b1;
@Column(nullable=false) @Column(nullable=false)
private int b2; private int b2;
@Column(name="SAME", unique=true)
private int same;
} }

View File

@ -65,7 +65,9 @@
<basic name="a6"> <basic name="a6">
<column name="a6x"/> <column name="a6x"/>
</basic> </basic>
<basic name="same">
<column name="SAME" unique="true"/>
</basic>
<basic name="sa1"> <basic name="sa1">
<column name="sa1x" table="UNIQUE_SECONDARY_XML" /> <column name="sa1x" table="UNIQUE_SECONDARY_XML" />
</basic> </basic>
@ -112,6 +114,9 @@
<basic name="b2"> <basic name="b2">
<column name="b2x"/> <column name="b2x"/>
</basic> </basic>
<basic name="same">
<column name="SAME" unique="true"/>
</basic>
</attributes> </attributes>
</entity> </entity>
</entity-mappings> </entity-mappings>