HHH-7797 Simplified UniqueKey creation and corrected a couple of

@UniqueConstraint bugs.
This commit is contained in:
Brett Meyer 2012-12-19 14:48:11 -05:00
parent 9c3998dade
commit 12c7ab93c3
6 changed files with 23 additions and 75 deletions

View File

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

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

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

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