HHH-7797 Simplified UniqueKey creation and corrected a couple of

@UniqueConstraint bugs.
Conflicts:
	hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java
This commit is contained in:
Brett Meyer 2012-12-19 14:53:46 -05:00
parent 61f548db6f
commit dc3222a4e3
6 changed files with 22 additions and 74 deletions

View File

@ -50,6 +50,7 @@ import java.util.StringTokenizer;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import javax.persistence.Embeddable; import javax.persistence.Embeddable;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.MapsId; import javax.persistence.MapsId;
@ -58,10 +59,6 @@ import org.dom4j.Attribute;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.dom4j.Element; import org.dom4j.Element;
import org.jboss.logging.Logger;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.hibernate.AnnotationException; import org.hibernate.AnnotationException;
import org.hibernate.DuplicateMappingException; import org.hibernate.DuplicateMappingException;
import org.hibernate.EmptyInterceptor; import org.hibernate.EmptyInterceptor;
@ -145,6 +142,9 @@ import org.hibernate.type.Type;
import org.hibernate.type.TypeResolver; import org.hibernate.type.TypeResolver;
import org.hibernate.usertype.CompositeUserType; import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType; import org.hibernate.usertype.UserType;
import org.jboss.logging.Logger;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
/** /**
* An instance of <tt>Configuration</tt> allows the application * An instance of <tt>Configuration</tt> allows the application
@ -1224,15 +1224,6 @@ public class Configuration implements Serializable {
) )
); );
} }
//broken, 'cos we don't generate these with names in SchemaExport
// subIter = table.getUniqueKeyIterator();
// while ( subIter.hasNext() ) {
// UniqueKey uk = (UniqueKey) subIter.next();
// if ( tableInfo==null || tableInfo.getIndexMetadata( uk.getFilterName() ) == null ) {
// script.add( uk.sqlCreateString(dialect, mapping) );
// }
// }
} }
} }
@ -1556,12 +1547,10 @@ public class Configuration implements Serializable {
unboundNoLogical.add( new Column( logicalColumnName ) ); unboundNoLogical.add( new Column( logicalColumnName ) );
} }
} }
UniqueKey uk = table.getOrCreateUniqueKey( keyName );
for ( Column column : columns ) { for ( Column column : columns ) {
if ( table.containsColumn( column ) ) { if ( table.containsColumn( column ) ) {
Column tableColumn = table.getColumn( column ); uk.addColumn( column );
tableColumn.setUnique( true );
Dialect.getDialect().getUniqueDelegate().generateUniqueKey(
table, tableColumn );
unbound.remove( column ); unbound.remove( column );
} }
} }

View File

@ -41,21 +41,6 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
this.dialect = dialect; this.dialect = dialect;
} }
@Override
public void generateUniqueKey( org.hibernate.mapping.Table table,
org.hibernate.mapping.Column column ) {
org.hibernate.mapping.UniqueKey uk = table.getOrCreateUniqueKey(
column.getQuotedName( dialect ) + '_' );
uk.addColumn( column );
}
@Override
public void generateUniqueKey( Table table, Column column ) {
UniqueKey uk = table.getOrCreateUniqueKey( column.getColumnName()
.encloseInQuotesIfQuoted( dialect ) + '_' );
uk.addColumn( column );
}
@Override @Override
public String applyUniqueToColumn( org.hibernate.mapping.Column column ) { public String applyUniqueToColumn( org.hibernate.mapping.Column column ) {
return ""; return "";

View File

@ -44,25 +44,6 @@ import org.hibernate.metamodel.relational.UniqueKey;
*/ */
public interface UniqueDelegate { public interface UniqueDelegate {
/**
* If the dialect supports unique constraints, create and add a UniqueKey
* to the Table.
*
* @param table
* @param column
*/
public void generateUniqueKey( org.hibernate.mapping.Table table,
org.hibernate.mapping.Column column );
/**
* If the dialect supports unique constraints, create and add a UniqueKey
* to the Table.
*
* @param table
* @param column
*/
public void generateUniqueKey( Table table, Column column );
/** /**
* If the dialect does not supports unique constraints, this method should * If the dialect does not supports unique constraints, this method should
* return the syntax necessary to mutate the column definition * return the syntax necessary to mutate the column definition

View File

@ -423,8 +423,9 @@ public class Table implements RelationalModel, Serializable {
} }
if ( column.isUnique() ) { if ( column.isUnique() ) {
dialect.getUniqueDelegate().generateUniqueKey( UniqueKey uk = getOrCreateUniqueKey(
this, column ); column.getQuotedName( dialect ) + '_' );
uk.addColumn( column );
alter.append( dialect.getUniqueDelegate() alter.append( dialect.getUniqueDelegate()
.applyUniqueToColumn( column ) ); .applyUniqueToColumn( column ) );
} }
@ -525,7 +526,9 @@ public class Table implements RelationalModel, Serializable {
} }
if ( col.isUnique() ) { if ( col.isUnique() ) {
dialect.getUniqueDelegate().generateUniqueKey( this, col ); UniqueKey uk = getOrCreateUniqueKey(
col.getQuotedName( dialect ) + '_' );
uk.addColumn( col );
buf.append( dialect.getUniqueDelegate() buf.append( dialect.getUniqueDelegate()
.applyUniqueToColumn( col ) ); .applyUniqueToColumn( col ) );
} }

View File

@ -200,7 +200,9 @@ public class Table extends AbstractTableSpecification implements Exportable {
} }
if ( col.isUnique() ) { if ( col.isUnique() ) {
dialect.getUniqueDelegate().generateUniqueKey( this, col ); UniqueKey uk = getOrCreateUniqueKey( col.getColumnName()
.encloseInQuotesIfQuoted( dialect ) + '_' );
uk.addColumn( col );
buf.append( dialect.getUniqueDelegate() buf.append( dialect.getUniqueDelegate()
.applyUniqueToColumn( col ) ); .applyUniqueToColumn( col ) );
} }

View File

@ -190,7 +190,6 @@ public class EntityTest extends BaseCoreFunctionalTestCase {
int id = 5; int id = 5;
Session s; Session s;
Transaction tx; Transaction tx;
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
Sky sky = new Sky(); Sky sky = new Sky();
@ -198,9 +197,6 @@ public class EntityTest extends BaseCoreFunctionalTestCase {
sky.color = "green"; sky.color = "green";
sky.day = "monday"; sky.day = "monday";
sky.month = "March"; sky.month = "March";
s.persist( sky );
tx.commit();
s.close();
Sky otherSky = new Sky(); Sky otherSky = new Sky();
otherSky.id = new Long( id++ ); otherSky.id = new Long( id++ );
@ -214,25 +210,17 @@ public class EntityTest extends BaseCoreFunctionalTestCase {
sameSky.day = "monday"; sameSky.day = "monday";
sameSky.month = "March"; sameSky.month = "March";
s = openSession(); s.save( sky );
tx = s.beginTransaction(); s.flush();
try {
s.persist( otherSky ); s.save( otherSky );
tx.commit(); tx.commit();
fail( "unique constraints not respected" );
}
catch (HibernateException e) {
//success
}
finally {
if ( tx != null ) tx.rollback();
s.close(); s.close();
}
s = openSession(); s = openSession();
tx = s.beginTransaction(); tx = s.beginTransaction();
try { try {
s.persist( sameSky ); s.save( sameSky );
tx.commit(); tx.commit();
fail( "unique constraints not respected" ); fail( "unique constraints not respected" );
} }