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.jar.JarFile;
import java.util.zip.ZipEntry;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.MapsId;
@ -58,10 +59,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.DuplicateMappingException;
import org.hibernate.EmptyInterceptor;
@ -145,6 +142,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 <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 ) );
}
}
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 );
}
}

View File

@ -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 ) {

View File

@ -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

View File

@ -423,8 +423,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 ) );
}
@ -525,7 +526,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 ) );
}

View File

@ -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 ) );
}

View File

@ -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" );
}