diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java index 48dfaf6e1c..f8391e1455 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java @@ -51,6 +51,7 @@ import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; import java.util.jar.JarFile; import java.util.zip.ZipEntry; + import javax.persistence.AttributeConverter; import javax.persistence.Embeddable; import javax.persistence.Entity; @@ -60,10 +61,6 @@ import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; 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.AssertionFailure; import org.hibernate.DuplicateMappingException; @@ -82,6 +79,7 @@ import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.java.JavaReflectionManager; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.cfg.annotations.reflection.JPAMetadataProvider; import org.hibernate.context.spi.CurrentTenantIdentifierResolver; import org.hibernate.dialect.Dialect; @@ -137,7 +135,6 @@ import org.hibernate.mapping.UniqueKey; import org.hibernate.proxy.EntityNotFoundDelegate; import org.hibernate.secure.internal.JACCConfiguration; import org.hibernate.service.ServiceRegistry; -import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.tool.hbm2ddl.DatabaseMetadata; import org.hibernate.tool.hbm2ddl.IndexMetadata; import org.hibernate.tool.hbm2ddl.TableMetadata; @@ -148,6 +145,9 @@ import org.hibernate.type.Type; import org.hibernate.type.TypeResolver; import org.hibernate.usertype.CompositeUserType; import org.hibernate.usertype.UserType; +import org.jboss.logging.Logger; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; /** * An instance of Configuration allows the application @@ -1228,15 +1228,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) ); -// } -// } } } @@ -1560,12 +1551,10 @@ public class Configuration implements Serializable { unboundNoLogical.add( new Column( logicalColumnName ) ); } } + UniqueKey uk = table.getOrCreateUniqueKey( keyName ); for ( Column column : columns ) { if ( table.containsColumn( column ) ) { - Column tableColumn = table.getColumn( column ); - tableColumn.setUnique( true ); - Dialect.getDialect().getUniqueDelegate().generateUniqueKey( - table, tableColumn ); + uk.addColumn( column ); unbound.remove( column ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java b/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java index 41678aa5b3..09e1d7e75d 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java @@ -40,21 +40,6 @@ public class DefaultUniqueDelegate implements UniqueDelegate { public DefaultUniqueDelegate( 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 public String applyUniqueToColumn( org.hibernate.mapping.Column column ) { diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/unique/UniqueDelegate.java b/hibernate-core/src/main/java/org/hibernate/dialect/unique/UniqueDelegate.java index ef3728361d..4cd3c346d6 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/unique/UniqueDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/unique/UniqueDelegate.java @@ -44,25 +44,6 @@ import org.hibernate.metamodel.relational.UniqueKey; */ 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 * return the syntax necessary to mutate the column definition diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java index 8aaf8e0064..ebd6329aa8 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java @@ -422,8 +422,9 @@ public class Table implements RelationalModel, Serializable { } if ( column.isUnique() ) { - dialect.getUniqueDelegate().generateUniqueKey( - this, column ); + UniqueKey uk = getOrCreateUniqueKey( + column.getQuotedName( dialect ) + '_' ); + uk.addColumn( column ); alter.append( dialect.getUniqueDelegate() .applyUniqueToColumn( column ) ); } @@ -524,7 +525,9 @@ public class Table implements RelationalModel, Serializable { } if ( col.isUnique() ) { - dialect.getUniqueDelegate().generateUniqueKey( this, col ); + UniqueKey uk = getOrCreateUniqueKey( + col.getQuotedName( dialect ) + '_' ); + uk.addColumn( col ); buf.append( dialect.getUniqueDelegate() .applyUniqueToColumn( col ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java index 69cdb3cf0e..d106685c3c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java @@ -200,7 +200,9 @@ public class Table extends AbstractTableSpecification implements Exportable { } if ( col.isUnique() ) { - dialect.getUniqueDelegate().generateUniqueKey( this, col ); + UniqueKey uk = getOrCreateUniqueKey( col.getColumnName() + .encloseInQuotesIfQuoted( dialect ) + '_' ); + uk.addColumn( col ); buf.append( dialect.getUniqueDelegate() .applyUniqueToColumn( col ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/EntityTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/EntityTest.java index 877461835c..9fbdfa7187 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/EntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/EntityTest.java @@ -190,7 +190,6 @@ public class EntityTest extends BaseCoreFunctionalTestCase { int id = 5; Session s; Transaction tx; - s = openSession(); tx = s.beginTransaction(); Sky sky = new Sky(); @@ -198,9 +197,6 @@ public class EntityTest extends BaseCoreFunctionalTestCase { sky.color = "green"; sky.day = "monday"; sky.month = "March"; - s.persist( sky ); - tx.commit(); - s.close(); Sky otherSky = new Sky(); otherSky.id = new Long( id++ ); @@ -214,25 +210,17 @@ public class EntityTest extends BaseCoreFunctionalTestCase { sameSky.day = "monday"; sameSky.month = "March"; - s = openSession(); - tx = s.beginTransaction(); - try { - s.persist( otherSky ); - tx.commit(); - fail( "unique constraints not respected" ); - } - catch (HibernateException e) { - //success - } - finally { - if ( tx != null ) tx.rollback(); - s.close(); - } + s.save( sky ); + s.flush(); + + s.save( otherSky ); + tx.commit(); + s.close(); s = openSession(); tx = s.beginTransaction(); try { - s.persist( sameSky ); + s.save( sameSky ); tx.commit(); fail( "unique constraints not respected" ); }